[英]Clamping problems with diagonal camera rotation around object based on mouse input
[英]Need help clamping camera rotation
我對編碼真的很陌生,並制作了這段代碼來圍繞播放器對象旋轉我的相機,但我不知道如何繼續鉗制 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);
}
}
這是我旋轉相機的代碼,我想夾住鼠標 Y。
這里的技巧是在進行轉換之前跟蹤累積的輸入並對其進行鉗制。 這比嘗試將相機的旋轉分解為正確的軸更容易且不易出錯。
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.