提问者:小点点

夹具Z旋转


这是我相当简单的代码,基本上是围绕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);
   }
}

共1个答案

匿名用户

如果使用相对于世界轴旋转,则无法锁定局部轴Z。它的方位在对世界轴心的旋转方面固有地移动。

我认为你可能需要的是,相对于物体的局部轴旋转。分别使用本地对象up和right,而不是world轴,如下所示:

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取向也会随着一个轴的取向而变化,涉及到另外两个轴的取向变化。