存代码父子相对关系

改变:在运动过程中设置了相对位置为(1,0,0).造成效果:旋转。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



//在预制体实例化与C sharp类的方法之间出现了矛盾。

public class body_generate : MonoBehaviour
{
    public GameObject prefab;
    private Vector3 oldpos;
    public Rigidbody rd;
    //public Transform self;

    List<GameObject> allbody;
    // Start is called before the first frame update
    void Start()
    {
        allbody = new List<GameObject>();
        allbody.Add(this.gameObject);
        rd = this.gameObject.transform.GetComponent<Rigidbody>();
        // GameObject first = new GameObject("one");
        // GameObject last = GameObject.Instantiate(prefab);
        //last.AddComponent(typeof(Rigidbody));
        // allbody.Add(last);
    }


    void Update()
    {
        Transform self = allbody[0].transform;

        if (Input.GetKeyDown(KeyCode.Q))
        {
          
            //施加力的方式
            oldpos = allbody[0].transform.position;
            allbody[0].transform.position = oldpos + new Vector3(1, 0, 0);
            /*
            for (int i = 0; i < allbody.Count; i++)
            {
                
                oldpos = allbody[i].transform.position;
                allbody[i].transform.position = oldpos + new Vector3(1, 0, 0);
            }*/
            if(allbody[allbody.Count - 1].transform.position[0] >= 1)
            {
                GameObject last = GameObject.Instantiate(prefab);
                last.AddComponent(typeof(Rigidbody));
                last.transform.parent = allbody[allbody.Count - 1].transform;
                last.transform.localPosition = new Vector3(1, 0, 0);//设置位置    已实现不碰撞的固定位置的实例化。
                last.transform.localRotation = Quaternion.identity;//设置角度
                allbody.Add(last);
            }
        }
        
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        //Debug.Log(h);
        //rd.AddForce(new Vector3(h, 0, v));
        for (int i = 1; i < allbody.Count; i++)
        {
            allbody[i].transform.localPosition = new Vector3(1, 0, 0);
            allbody[i].transform.localRotation = Quaternion.identity;
        }
        // GameObject third = GameObject.CreatePrimitive(PrimitiveType.Capsule);
    }
}

也添加了testforce给预制体,test代码同上。复制在下面。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class testforce : MonoBehaviour
{
    public Rigidbody rd;


    // Start is called before the first frame update
    void Start()
    {
        //this.gameObject.AddComponent(typeof(Rigidbody));
        rd = this.gameObject.transform.GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
       
        if (Input.GetKeyDown(KeyCode.K))
        {          
            rd.AddForce(Vector3.right);
        }
        if (Input.GetKeyDown(KeyCode.H))
        {
            rd.AddForce(Vector3.left);
        }
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        //Debug.Log(h);
        rd.AddForce(new Vector3(h, 0, v));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值