模型移动

   
   
   
   
  1. float distance = Mathf.Abs(Vector3.Distance(hit.point, transform.position)); 
  2. this.targetPoint = hit.point; 
  3. this.moveTime = distance / MoveSpeed; 
  4. this.targetForward = (targetPoint - this.transform.position).normalized;
     

模型的旋转

可以用unity自带的四元数实现  

   
   
   
   
  1. Quaternion from = this.transform.rotation; 
  2. Quaternion to = Quaternion.FromToRotation(this.targetForward, this.transform.forward); 
  3. Quaternion.Slerp(from, to, time); 

但这段代码可以用更简单高效的办法实现

   
   
   
   
  1. float degrees  = Vector3.Angle(this.targetForward, this.transform.forward); 
  2. Vector3 cross = Vector3.Cross(this.targetForward, this.transform.forward); 
  3. this.rotateSpeed = degrees / RotateTime; 
  4.  
  5. if (cross.y >= 0) //角度正负
  6.     this.rotateSpeed *= -1; 
  7.  
  8. this.rotateTime = RotateTime; 

 最后移动和旋转

   
   
   
   
  1. void MoveAndRotate() 
  2.        if (this.rotateTime > 0) 
  3.        { 
  4.            this.rotateTime -= Time.deltaTime; 
  5.  
  6.            this.transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed); 
  7.  
  8.            if (this.rotateTime <= 0) 
  9.            { 
  10.                this.transform.forward = this.targetForward; 
  11.            } 
  12.        } 
  13.  
  14.        if (this.moveTime > 0) 
  15.        { 
  16.            this.transform.position +=  this.targetForward * MoveSpeed * Time.deltaTime; 
  17.  
  18.            this.moveTime -= Time.deltaTime; 
  19.            if (this.moveTime <= 0) 
  20.            { 
  21.                this.transform.position = this.targetPoint; 
  22.                this.SetState(PlayerState.idle); 
  23.            } 
  24.        }