Unity 第三人称游戏中角色旋转代码
官方第三人称Demo中角色旋转代码,角色根据相机角度转向
1: 获取用户移动的变量 Vector3 inputDirection = new Vector3(_input.move.x, 0.0f, _input.move.y).normalized; 2: 转换成目标角度 角色需要旋转的角度 _targetRotation = Mathf.Atan2(inputDirection.x, inputDirection.z) * Mathf.Rad2Deg + _mainCamera.transform.eulerAngles.y; 3:把当前角度渐变成目标角度 float rotation = Mathf.SmoothDampAngle(transform.eulerAngles.y, _targetRotation, ref _rotationVelocity, RotationSmoothTime); 4:把最终的角度赋值给角色 transform.rotation = Quaternion.Euler(0.0f, rotation, 0.0f);