Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 夹紧Z旋转_C#_Unity3d - Fatal编程技术网

C# 夹紧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

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

如果使用
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方向也会发生变化,因为相对于一个轴的方向涉及到其他两个轴的方向变化