unity的三种物体移动方法

来源:互联网 发布:黑龙江省快乐十分软件 编辑:程序博客网 时间:2024/05/22 15:41

1.带有Rigidbody组件的物体移动一般采用 GetComponent<Rigidbody>().velocity  如:

         float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        if(Input.GetButtonDown("Jump"))
            vel = jump;
         else
            vel = GetComponent<Rigidbody>().velocity.y;
        Vector3 movement = new Vector3(h * speed, vel, v * speed);

        movement=Vector3.ClampMagnitude(movement,speed);//限制对角移动的速度和沿着轴线一样
        movement = transform.TransformDirection(movement);//因为velocity是相对全局坐标而言的,所以要把局部坐标转换成全局坐标 如:往左转是相对于移动的物体的 
        GetComponent<Rigidbody>().velocity =movement;

2.带有CharacterController组件的物体一般采用Move(movement)  如:

          Vector3 movement = new Vector3(h * speed, -9.8f, v * speed);
          movement = Vector3.ClampMagnitude(movement, speed);
          movement *= Time.deltaTime;
          movement = transform.TransformDirection(movement);//因为Move是相对全局坐标而言的,所以要把局部坐标转换成全局坐标 如:往左转是相对于移动的物体的 
         _characterController.Move(movement);

3.什么都不带的物体移动一般采用transform.Translate(x,y,z)  如:

注:transform.Translate的移动是不受物理作用的 即:可以穿墙   所以如果不是在平地上移动是不能用translate的

           transform.Translate(movement);//因为一般物体的移动就是在母坐标(不一定是全局如果它有母物体的话)下移动,所以一般不需要用TransformDirection(movement)转换

         



0 0
原创粉丝点击