[Unity3D self-study record] Character control in the free perspective state of Unity3D game development (2)

 When testing the character control project in the free perspective state of Unity3D game development, I accidentally found a bug. The bug appears in the following location: [csharp] view plain copy print ? 

  1. //Set the player's follow angle  
  2. if(Target.GetComponent<NoLockiVew_Player>().State==NoLockiVew_Player.PlayerState.Walk)  
  3. {  
  4.     Target.rotation=Quaternion.Euler(new Vector3(0,mX,0));  
  5. }  

The main function of this method is that when the player presses the direction control key and the right mouse button at the same time, the player can rotate to the corresponding angle with the mouse. This is mainly to meet the needs of the player's two-handed operation. However, due to this line of code, the player's turning in the three directions of left, right, and backward fails. Later, I thought that our rotation of the character should actually be placed in the right mouse button event, so I modified the code as follows, which solved the bug:


  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class NoLockView_Camera : MonoBehaviour   
  5. {  
  6.     //observe target  
  7.     public Transform Target;  
  8.     //observation distance  
  9.     public float Distance = 5F;  
  10.     //spinning speed  
  11.     private float SpeedX=240;  
  12.     private float SpeedY=120;  
  13.     // angle limit  
  14.     private float  MinLimitY = 5;  
  15.     private float  MaxLimitY = 180;  
  16.   
  17.     //Rotation angle  
  18.     private float mX = 0.0F;  
  19.     private float mY = 0.0F;  
  20.   
  21.     //Mouse zoom distance maximum value  
  22.     private float MaxDistance=10;  
  23.     private float MinDistance=1.5F;  
  24.     // mouse zoom rate  
  25.     private float ZoomSpeed=2F;  
  26.   
  27.     // Whether to enable difference  
  28.     public bool isNeedDamping=true;  
  29.     //speed  
  30.     public float Damping=10F;  
  31.   
  32.     private Quaternion mRotation;  
  33.   
  34.     void Start ()   
  35.     {  
  36.         //初始化旋转角度  
  37.         mX=transform.eulerAngles.x;  
  38.         mY=transform.eulerAngles.y;  
  39.     }  
  40.       
  41.     void LateUpdate ()   
  42.     {  
  43.         //鼠标右键旋转  
  44.         if(Target!=null && Input.GetMouseButton(1))  
  45.         {  
  46.             //获取鼠标输入  
  47.             mX += Input.GetAxis("Mouse X") * SpeedX * 0.02F;  
  48.             mY -= Input.GetAxis("Mouse Y") * SpeedY * 0.02F;  
  49.             //范围限制  
  50.             mY = ClampAngle(mY,MinLimitY,MaxLimitY);  
  51.             //计算旋转  
  52.             mRotation = Quaternion.Euler(mY, mX, 0);  
  53.             //根据是否插值采取不同的角度计算方式  
  54.             if(isNeedDamping){  
  55.                 transform.rotation = Quaternion.Lerp(transform.rotation,mRotation, Time.deltaTime*Damping);   
  56.             }else{  
  57.                 transform.rotation = mRotation;  
  58.             }  
  59.             //处理同时按下鼠标右键和方向控制键  
  60.             if(Target.GetComponent<NoLockiVew_Player>().State==NoLockiVew_Player.PlayerState.Walk){  
  61.                 Target.rotation=Quaternion.Euler(new Vector3(0,mX,0));  
  62.             }  
  63.         }  
  64.   
  65.         //鼠标滚轮缩放  
  66.         Distance-=Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;  
  67.         Distance=Mathf.Clamp(Distance,MinDistance,MaxDistance);  
  68.   
  69.         //重新计算位置  
  70.         Vector3 mPosition = mRotation * new Vector3(0.0F, 0.0F, -Distance) + Target.position;  
  71.   
  72.         //设置相机的角度和位置  
  73.         if(isNeedDamping){  
  74.            transform.position = Vector3.Lerp(transform.position,mPosition, Time.deltaTime*Damping);   
  75.         }else{  
  76.            transform.position = mPosition;  
  77.         }  
  78.   
  79.     }  
  80.   
  81.   
  82.     //角度限制  
  83.     private float  ClampAngle (float angle,float min,float max)   
  84.     {  
  85.         if (angle < -360) angle += 360;  
  86.         if (angle >  360) angle -= 360;  
  87.         return Mathf.Clamp (angle, min, max);  
  88.     }  
  89. }  
不过经过测试,如果不采用插值的话,似乎效果更为真实啊(为什么会和第一次测试的感觉不一样啊,囧!)


Guess you like

Origin blog.csdn.net/hackdjh/article/details/39893931