Rigidbody body = GetComponent<Rigidbody>();
body.freezeRotation = true;//禁止刚体旋转,FPSInput.cs
bool Input.GetAxis("Mouse Y") //鼠标往前后推,MouseLook.cs
bool Input.GetAxis("Mouse X")//鼠标往左右移动,MouseLook.cs
bool Input.GetAxis("Horizontal")//WS ↕ FPSInput.cs
bool Input.GetAxis("Vertical") //AD ↔ FPSInput.cs
bool Input.GetKeyDown(KeyCode.X)//输入X就xxx FPSInput.cs
float _rotationX=Mathf.Clamp(_rotationX, minhead, maxhead);//限制范围【minhead,maxhead】
MouseLook.cs
move = Vector3.ClampMagnitude(move, speed)//限制向量长度 FPSInput.cs
transform.Rotate(0, Input.GetAxis("Mouse X")*speedH, 0)//增量旋转 MouseLook.cs
transform.localEulerAngles=new Vector3( _rotationX, _rotationY,0);//设置旋转 MouseLook.cs
transform.localEulerAngles.y//获取当前的y轴旋转角度
move = transform.TransformDirection(move);//本地转全局变量
CharacterController _charController = GetComponent<CharacterController>();
_charController.Move(//Vector3 move* Time.deltaTime);//增量移动
_charController.isGrounded
本文详细介绍了如何在Unity中使用Rigidbody组件控制第一人称射击(FPS)游戏的角色移动与旋转。通过设置freezeRotation属性禁用刚体旋转,并利用Input.GetAxis获取玩家输入,实现平滑的移动与视角调整。此外,还介绍了如何限制旋转角度和移动速度。
2万+

被折叠的 条评论
为什么被折叠?



