Unity3D物体移动及Player移动的通用方法

物体移动的常用方法

一、transform.Translate

[csharp] view plain copy
  1. transform.Translate(Vector3.forward *MoveSpeed * Time.deltaTime, Space.transform);    

二、velocity(刚体)

[csharp] view plain copy
  1. gameObject.GetComponent<Rigidbody>().velocity = Vector3.forward * MoveSpeed;    

三、rigbody.MovePosition()

[csharp] view plain copy
  1. //让物体向前运动Time.deltaTime距离    
  2.         rb.MovePosition(transform.position + transform.forward * Time.deltaTime);    

四、Vector3.MoveTowards()

[csharp] view plain copy
  1. public int MoveSpeed = 10;    
  2.     Vector3 target;    
  3.     
  4.      void Start () {    
  5.         target = new Vector3(20, transform.position.y, 20);    
  6.      }    
  7.      
  8.      void Update () {    
  9.         transform.position = Vector3.MoveTowards(transform.position, target, MoveSpeed * Time.deltaTime);    
  10.      }    

五、Lerp()

1.使用Mathf.Lerp()函数

[csharp] view plain copy
  1. gameObject.transform.localPosition = new Vector3(    
  2.         Mathf.Lerp(transform.position.x, Target.x, MoveSpeed * Time.deltaTime),    
  3.         Mathf.Lerp(transform.position.y, Target.y, MoveSpeed * Time.deltaTime),    
  4.         Mathf.Lerp(transform.position.z, Target.z, MoveSpeed * Time.deltaTime));    

2.使用Vector3.Lerp()

[csharp] view plain copy
  1. gameObject.transform.localPosition = Vector3.Lerp(transform.position, Target, MoveSpeed * Time.deltaTime);  

六、使用SmoothDamp()  平滑阻尼(摄像机)

[csharp] view plain copy
  1. //平滑地移动摄像机朝向目标位置      smoothTime:大约花费时间  
  2.        transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);    

Player移动的常用方法

一、rigidbody.MovePosition() 控制物体上下左右移动

[csharp] view plain copy
  1. float h = Input.GetAxis("Horizontal");    
  2.        float v = Input.GetAxis("Vertical");    
  3.    
  4.        rigidbody.MovePosition(transform.position + new Vector3(h, 0, v) * speed * Time.deltaTime);    

二、rigidbody.velocity控制物体 上下左右移动(坦克大战,控制物体转向移动)

[csharp] view plain copy
  1. //获取轴    每个轴对应于键盘或者鼠标的某种动作  Horizontal为x轴   Vertical为z轴  
  2.       float v = Input.GetAxis("VerticalPlay"+Number);  
  3.       //刚体速度向量      z轴向前*  
  4.       m_rigidbody.velocity = transform.forward * v * speed;  
  5.   
  6.       float h = Input.GetAxis("HorizontalPlay"+Number);  
  7.       //刚体角度速度      
  8.       m_rigidbody.angularVelocity = transform.up * h * angularSpeed;  

三、transform.Translate()上下左右移动

[csharp] view plain copy
  1. float h = Input.GetAxis("Horizontal");    
  2.         float v = Input.GetAxis("Vertical");    
  3.     
  4.         transform.Translate(new Vector3(h, 0, v) * speed * Time.deltaTime);    

四、点击鼠标,物体移动到鼠标的位置

[csharp] view plain copy
  1. protected Transform _transform;    
  2. protected Vector3 targetPos;//目标位置    
  3.     
  4.     // Use this for initialization    
  5.     void Start()    
  6.     {    
  7.         _transform = this.transform;    
  8.         targetPos = this._transform.position;    
  9.     }    
  10.     
  11.     void MoveTo()    
  12.     {    
  13.         if (Input.GetMouseButton(0))    
  14.         {    
  15.             //获得鼠标屏幕位置    
  16.             Vector3 mousePos = Input.mousePosition;    
  17.             //将屏幕位置转为射线    
  18.             Ray ray = Camera.main.ScreenPointToRay(mousePos);    
  19.             //用来记录射线碰撞记录    
  20.             RaycastHit hitInfo;    
  21.             //产生射线    
  22.             bool isCast = Physics.Raycast(ray, out hitInfo, 1000, inputMask);    
  23.             if (isCast)    
  24.             {    
  25.                 //如果射中目标,记录射线碰撞点     
  26.                 targetPos = hitInfo.point;    
  27.             }    
  28.         }    
  29.         //使用Vector3提供的MoveTowards函数,获得朝目标移动的位置    
  30.         Vector3 pos = Vector3.MoveTowards(this._transform.position, targetPos, speed * Time.deltaTime);    
  31.         //更新当前位置    
  32.         this._transform.position = pos;    
  33.     }    

五、rigidbody2D.AddForce()  2D游戏添加力左右移动物体(忍者跑酷)

[csharp] view plain copy
  1. if (isSlide == false)    
  2.         {    
  3.             float h = Input.GetAxis("Horizontal");    
  4.             Vector2 velocity = rigidbody2D.velocity;    
  5.     
  6.     
  7.             if (h > 0.05f)    
  8.             {    
  9.                 rigidbody2D.AddForce(Vector2.right * force_move);    
  10.             }    
  11.             else if (h < -0.05f)    
  12.             {    
  13.                 rigidbody2D.AddForce(-Vector2.right * force_move);    
  14.             }    

六、CharacterController组建

1.Move移动

[csharp] view plain copy
  1. public float speed = 6.0F;  
  2.   
  3.     public float jumpSpeed = 8.0F;  
  4.   
  5.     public float gravity = 20.0F;  
  6.   
  7.     private Vector3 moveDirection = Vector3.zero;  
  8.   
  9.     void Update() {  
  10.   
  11.         CharacterController controller = GetComponent<CharacterController>();  
  12.   
  13.         if (controller.isGrounded) {  
  14.   
  15.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));  
  16.   
  17.             moveDirection = transform.TransformDirection(moveDirection);  
  18.   
  19.             moveDirection *= speed;  
  20.   
  21.             if (Input.GetButton("Jump"))  
  22.   
  23.                 moveDirection.y = jumpSpeed;  
  24.   
  25.    
  26.   
  27.         }  
  28.   
  29.         moveDirection.y -= gravity * Time.deltaTime;  
  30.   
  31.         controller.Move(moveDirection * Time.deltaTime);  
  32.   
  33.     }  

2.SimpleMove移动

[csharp] view plain copy
  1. public float speed = 3.0F;  
  2.   
  3.     public float rotateSpeed = 3.0F;  
  4.   
  5.     void Update() {  
  6.   
  7.         CharacterController controller = GetComponent<CharacterController>();  
  8.   
  9.         transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);  
  10.   
  11.         Vector3 forward = transform.TransformDirection(Vector3.forward);  
  12.   
  13.         float curSpeed = speed * Input.GetAxis("Vertical");  
  14.   
  15.         controller.SimpleMove(forward * curSpeed);  
  16.   
  17.     }  

两者区别

Unity3D物体移动及Player移动的通用方法