C# 夹紧Z旋转
这是我非常简单的代码,基本上是围绕x和y上的目标旋转。但如果我同时按下这两个按钮,它会在z轴上旋转,翻转相机。有没有办法锁定z轴C# 夹紧Z旋转,c#,unity3d,C#,Unity3d,这是我非常简单的代码,基本上是围绕x和y上的目标旋转。但如果我同时按下这两个按钮,它会在z轴上旋转,翻转相机。有没有办法锁定z轴 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Camera_Logic : MonoBehaviour { public float rotateSpeed; public Transform rotateTarge
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);
}
}
如果使用
Vector3.up
和Vector3.right
相对于世界轴旋转,则无法锁定局部轴z。其方向相对于世界轴旋转固有地移动
我认为你可能需要的是旋转物体的局部轴。
不使用世界轴Vector3.up
和Vector3.right
分别使用局部对象up和right,如下所示:
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方向也会发生变化,因为相对于一个轴的方向涉及到其他两个轴的方向变化