[英]Need help clamping camera rotation
I am really new to coding and made this code to rotate my camera around the player object but I do not know how I would proceed to clamp the rotation on the X axis.我对编码真的很陌生,并制作了这段代码来围绕播放器对象旋转我的相机,但我不知道如何继续钳制 X 轴上的旋转。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public float speed;
public Transform target;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.RotateAround(target.position, transform.right, -Input.GetAxis("Mouse Y") * speed);
transform.RotateAround(target.position, Vector3.up, -Input.GetAxis("Mouse X") * speed);
}
}
This is my code to rotate the camera, I want to clamp the Mouse Y one.这是我旋转相机的代码,我想夹住鼠标 Y。
The trick here is to keep track of the accumulated input and clamping that before doing the transformations.这里的技巧是在进行转换之前跟踪累积的输入并对其进行钳制。 This is easier and less error-prone than trying to decompose the camera's rotation into the correct axes.这比尝试将相机的旋转分解为正确的轴更容易且不易出错。
public class FollowPlayer : MonoBehaviour
{
public float speed;
public Transform target;
float vertical;
float horizontal;
Quaternion initalRotation;
Vector3 initialOffset;
// Start is called before the first frame update
void Start()
{
initialRotation = transform.rotation;
initialOffset = transform.position - target.position;
}
// Update is called once per frame
void Update()
{
horizontal += - Input.GetAxis("Mouse X") * speed;
vertical += - Input.GetAxis("Mouse Y") * speed;
vertical = Mathf.Clamp(vertical, -20f, 20f);
//always rotate from same origin
transform.rotation = initialRotation;
transform.position = target.position + initialOffset;
transform.RotateAround(target.position, transform.right, vertical);
transform.RotateAround(target.position, Vector3.up, horizontal);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.