这是我相当简单的代码,基本上是围绕x和Y上的一个目标旋转。但如果我同时按下这两个键,它就会在z上旋转,翻转相机。有没有办法可以锁定z轴?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera_Logic : MonoBehaviour
{
public float rotateSpeed;
public Transform rotateTarget;
void Start()
{
}
void Update()
{
float rotateAxisX = Input.GetAxisRaw("Horizontal");
float rotateAxisY = Input.GetAxisRaw("Vertical");
transform.RotateAround(rotateTarget.transform.position, Vector3.up * -rotateAxisX, rotateSpeed * Time.deltaTime);
transform.RotateAround(rotateTarget.transform.position, Vector3.right * rotateAxisY, rotateSpeed * Time.deltaTime);
}
}
如果使用
我认为你可能需要的是,相对于物体的局部轴旋转。
void Update()
{
float rotateAxisX = Input.GetAxisRaw("Horizontal");
float rotateAxisY = Input.GetAxisRaw("Vertical");
transform.RotateAround(rotateTarget.transform.position, rotateTarget.up * -rotateAxisX, rotateSpeed * Time.deltaTime);
transform.RotateAround(rotateTarget.transform.position, rotateTarget.right * rotateAxisY, rotateSpeed * Time.deltaTime);
}
这样你就可以在物体z轴上反转旋转。然而z取向也会随着一个轴的取向而变化,涉及到另外两个轴的取向变化。