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 ?
- //Set the player's follow angle
- if(Target.GetComponent<NoLockiVew_Player>().State==NoLockiVew_Player.PlayerState.Walk)
- {
- Target.rotation=Quaternion.Euler(new Vector3(0,mX,0));
- }
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:
- using UnityEngine;
- using System.Collections;
- public class NoLockView_Camera : MonoBehaviour
- {
- //observe target
- public Transform Target;
- //observation distance
- public float Distance = 5F;
- //spinning speed
- private float SpeedX=240;
- private float SpeedY=120;
- // angle limit
- private float MinLimitY = 5;
- private float MaxLimitY = 180;
- //Rotation angle
- private float mX = 0.0F;
- private float mY = 0.0F;
- //Mouse zoom distance maximum value
- private float MaxDistance=10;
- private float MinDistance=1.5F;
- // mouse zoom rate
- private float ZoomSpeed=2F;
- // Whether to enable difference
- public bool isNeedDamping=true;
- //speed
- public float Damping=10F;
- private Quaternion mRotation;
- void Start ()
- {
- //初始化旋转角度
- mX=transform.eulerAngles.x;
- mY=transform.eulerAngles.y;
- }
- void LateUpdate ()
- {
- //鼠标右键旋转
- if(Target!=null && Input.GetMouseButton(1))
- {
- //获取鼠标输入
- mX += Input.GetAxis("Mouse X") * SpeedX * 0.02F;
- mY -= Input.GetAxis("Mouse Y") * SpeedY * 0.02F;
- //范围限制
- mY = ClampAngle(mY,MinLimitY,MaxLimitY);
- //计算旋转
- mRotation = Quaternion.Euler(mY, mX, 0);
- //根据是否插值采取不同的角度计算方式
- if(isNeedDamping){
- transform.rotation = Quaternion.Lerp(transform.rotation,mRotation, Time.deltaTime*Damping);
- }else{
- transform.rotation = mRotation;
- }
- //处理同时按下鼠标右键和方向控制键
- if(Target.GetComponent<NoLockiVew_Player>().State==NoLockiVew_Player.PlayerState.Walk){
- Target.rotation=Quaternion.Euler(new Vector3(0,mX,0));
- }
- }
- //鼠标滚轮缩放
- Distance-=Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;
- Distance=Mathf.Clamp(Distance,MinDistance,MaxDistance);
- //重新计算位置
- Vector3 mPosition = mRotation * new Vector3(0.0F, 0.0F, -Distance) + Target.position;
- //设置相机的角度和位置
- if(isNeedDamping){
- transform.position = Vector3.Lerp(transform.position,mPosition, Time.deltaTime*Damping);
- }else{
- transform.position = mPosition;
- }
- }
- //角度限制
- private float ClampAngle (float angle,float min,float max)
- {
- if (angle < -360) angle += 360;
- if (angle > 360) angle -= 360;
- return Mathf.Clamp (angle, min, max);
- }
- }