如何限制相机在 Y 轴上的旋转?帮我 §
Posted
技术标签:
【中文标题】如何限制相机在 Y 轴上的旋转?帮我 §【英文标题】:How I can put limits on the rotation of the camera on the Y axis? Help me § 【发布时间】:2021-05-06 19:19:22 【问题描述】:我无法限制 Y 轴上的旋转,而且我会让我的玩家朝他移动的方向看,我已经尝试将 Mathf.calmp 放在Touchfiled.Touchdist.y(相机 Y 轴)但它不起作用我不知道问题出在哪里,但我想解决它,所以请帮助我。 这是我的代码:
public FloatingJoystick joystick;
public TouchField touchField;
public GameObject FocusPoint;
[HideInInspector] public CharacterController characterController;
[HideInInspector] public Vector3 motionVector , gravityVector, RelativeVector;
[Header("Player Settings")]
[Range(0,6)] public float PlayerSpeed = 2.5f;
[Range(0, 100)] public float Sensivility = 50;
[Range(0, 0.5f)] public float groundClearance;
private float gravityForce = -9.18f;
private float gravityPower = -9.8f;
private float JumpValue = 9.8f;
private float TurnDiraction;
private void Start()
characterController = GetComponent<CharacterController>();
private void Update()
Movment();
CameraController();
if (characterController.isGrounded)
print("Grounded");
public void CameraController()
RelativeVector = transform.InverseTransformPoint(FocusPoint.transform.position);
RelativeVector /= RelativeVector.magnitude;
TurnDiraction = (RelativeVector.x / RelativeVector.magnitude);
FocusPoint.transform.eulerAngles = new Vector3(FocusPoint.transform.eulerAngles.x + touchField.TouchDist.y, FocusPoint.transform.eulerAngles.y, 0) ;
FocusPoint.transform.parent.Rotate(transform.up * touchField.TouchDist.x * Sensivility * Time.deltaTime);
public void Movment()
if (characterController.isGrounded && gravityVector.y < 0)
gravityVector.y = -2;
gravityVector.y += gravityPower * Time.deltaTime;
characterController.Move(gravityVector * Time.deltaTime);
if (characterController.isGrounded)
motionVector = transform.right * joystick.Horizontal + transform.forward * joystick.Vertical;
characterController.Move(motionVector * PlayerSpeed * Time.deltaTime);
if(joystick.Vertical > 0)
transform.Rotate(transform.up * TurnDiraction * 800 * Time.deltaTime);
FocusPoint.transform.parent.Rotate(transform.up * -TurnDiraction * 800 * Time.deltaTime);
【问题讨论】:
【参考方案1】:您分两步旋转,然后在第二步和第二步中旋转:
FocusPoint.transform.parent.Rotate(transform.up * touchField.TouchDist.x * Sensivility * Time.deltaTime);
如果您旋转父对象,则所有子对象都沿 y 轴旋转,同时旋转给层次结构中的父对象。 所以你在上一步中夹住y旋转并不重要
FocusPoint.transform.eulerAngles = new Vector3(FocusPoint.transform.eulerAngles.x + touchField.TouchDist.y, FocusPoint.transform.eulerAngles.y, 0);
如果游戏对象之后要相对于 y 轴旋转。
尝试根据您的限制一次轮换,或在所有轮换步骤中考虑这些限制。
另一方面,重力既不是力量,也不是力,而是加速度:)
【讨论】:
以上是关于如何限制相机在 Y 轴上的旋转?帮我 §的主要内容,如果未能解决你的问题,请参考以下文章