在我的项目中,我目前有1个脚本,1个摄像头和1个对象 . 我写了一个代码(见下文),它会围绕物体旋转相机,但X轴并不总是左右,而Y轴并不总是上下 .

我的意思是,例如,如果我们加载到单位,相机轴为Y =向上,X =向左,Z为向前作为变换位置,对象具有与摄像机相同的方向 . 我可以向左或向右/向上或向下单击并向左拖动,相机将正确旋转,但只要我向任何方向旋转90°,相反的轴就不会按我的意愿旋转 .

我知道这个“完美轮换”有一个术语,但我不知道这个术语,因此我很难解释 . 抱歉 . 请随意将代码输入C#脚本并随意使用它 . 将脚本添加到摄像机,确保摄像机正在查看对象,并且需要将对象添加到脚本上的游戏对象中 .

有人可以帮助我,这样当我向左滑动时,物体将始终向左旋转,向右旋转,向上向上旋转向下旋转 .

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FocusObject : MonoBehaviour
{

    public GameObject target;//the target object
    private Vector3 point;//the coord to the point where the camera looks at

    float f_lastX = 0.0f;
    float f_difX = 0.0f;  
    float f_lastY = 0.0f;
    float f_difY = 0.0f;  


    void Start()
    {//Set up things on the start method
        point = target.transform.position;//get target's coords
        transform.LookAt(point);//makes the camera look to it 
    }

    void Update()
    {

        if (Input.GetMouseButtonDown(0))
        {
            f_difX = 0.0f;
            f_difY = 0.0f;
        }
        else if (Input.GetMouseButton(0))
        {
            f_difX = Mathf.Abs(f_lastX - Input.GetAxis("Mouse X"));
            f_difY = Mathf.Abs(f_lastY - Input.GetAxis("Mouse Y"));

            if (f_lastX < Input.GetAxis("Mouse X"))
            {
                transform.RotateAround(point, new Vector3(0.0f, 1.0f, 0.0f), f_difX);
            }

            if (f_lastX > Input.GetAxis("Mouse X"))
            {
                transform.RotateAround(point, new Vector3(0.0f, -1.0f, 0.0f), f_difX);
            }

            if (f_lastY < Input.GetAxis("Mouse Y"))
            {
                transform.RotateAround(point, new Vector3(1.0f, 0.0f, 0.0f), f_difY);
            }

            if (f_lastY > Input.GetAxis("Mouse Y"))
            {
                transform.RotateAround(point, new Vector3(-1.0f, 0.0f, 0.0f), f_difY);

            }

            f_lastX = -Input.GetAxis("Mouse X");
            f_lastY = -Input.GetAxis("Mouse Y");
        }
    }
}