问题是当运行游戏时,当标志bool变量rotateObjectItself在开始时为真或假,然后改变其状态,它也改变了X上的相机旋转.

当标志设置为true,如果相机在所有轴上围绕物体旋转,它的逻辑也看到天空,但当改变标志回false时,我希望它返回,X上的主相机旋转将返回到它的默认值,但旋转锁定在X上的值—84.

using UnityEngine;

public class GlobalRotation : MonoBehaviour
{
    public GameObject target;
    public Transform rotatingCamera;
    public float rotationSpeed = 60;
    public Vector3 cameraOffset = new Vector3(0, 5, -10); // Adjust this to get the best view
    public bool rotateAroundX = false;
    public bool rotateAroundY = true; // Default to Y-axis rotation
    public bool rotateAroundZ = false;
    public bool rotateObjectItself = false; // If true, rotate the object around itself, otherwise rotate the camera around the object

    private Transform objectToRotate;
    private Transform cameraTransform;

    void Start()
    {
        target = GameObject.Find("Cube");
        if (target == null)
        {
            Debug.LogError("Target Cube not found");
            return;
        }

        // Determine the transforms
        objectToRotate = target.transform;
        cameraTransform = rotatingCamera ? rotatingCamera : Camera.main.transform;
    }

    void LateUpdate()
    {
        // Determine the rotation axis based on the selected booleans
        Vector3 rotationAxis = new Vector3(rotateAroundX ? 1 : 0, rotateAroundY ? 1 : 0, rotateAroundZ ? 1 : 0);

        if (rotateObjectItself)
        {
            // Rotate the object around its own axis
            objectToRotate.Rotate(rotationAxis, rotationSpeed * Time.deltaTime);
            // Optionally keep the camera looking at the object
            if (cameraTransform != null)
            {
                cameraTransform.position = objectToRotate.position + cameraOffset;
                cameraTransform.LookAt(objectToRotate);
            }
        }
        else
        {
            // Rotate the camera around the target
            cameraTransform.RotateAround(target.transform.position, rotationAxis, rotationSpeed * Time.deltaTime);
            cameraTransform.LookAt(target.transform);
        }
    }
}

这张截图是当摄像机围绕立方体旋转时的画面.x上的主摄像头旋转是7.95

camera is rotating around the cube

现在我判断了国旗和相机不再旋转了,立方体是围绕着自己旋转.

现在摄像机也面向天空就像是从底部看立方体一样.我希望它能像一开始时相机绕着立方体旋转的样子.

object is rotating around itself, and the camera is now looking up to the sky

推荐答案

下面的脚本允许您根据判断器中的 Select 围绕其旋转轴旋转对象.此外,还可以使相机绕物体的全局y轴旋转.两种旋转可以同时发生.默认情况下,对象在播放时绕其局部y轴旋转.不要忘记在判断器中引用目标对象以及脚本组件上的相机:

using UnityEngine;

public class GlobalRotation : MonoBehaviour
{
    public Transform objectToRotate;
    public Transform rotatingCamera;

    [Tooltip("If true, rotate the object about LOCAL axis of object")]
    public bool rotateObject;
    [Tooltip("If true, rotate referenced camera around object about GLOBAL y-axis of object")]
    public bool rotateCam;

    [Tooltip("Enable if object should rotate about local x-axis. Has no effect if rotating camera around object.")]
    public bool rotateObjAroundX = false;
    [Tooltip("Enable if object should rotate about local y-axis. Has no effect if rotating camera around object.")]
    public bool rotateObjAroundY = true;
    [Tooltip("Enable if object should rotate about local z-axis. Has no effect if rotating camera around object.")]
    public bool rotateObjAroundZ = false;

    public float rotationSpeed = 60;

    // Camera rotates around GLOCAL y-axis of object
    private Vector3 camRotAxis = Vector3.up;


    void Start()
    {
        // Rotate object about local y-axis by default
        rotateObject = true;
        rotateCam = false;

        rotateObjAroundX = false;
        rotateObjAroundY = true;
        rotateObjAroundZ = false;
    }

    void Update()
    {
        // Check if rotating object
        if (rotateObject)
        {
            // Determine LOCAL rotation axis
            Vector3 localRotAxis = new Vector3(rotateObjAroundX ? 1 : 0, rotateObjAroundY ? 1 : 0, rotateObjAroundZ ? 1 : 0);

            // Rotate the object around its own axis
            objectToRotate.Rotate(localRotAxis, rotationSpeed * Time.deltaTime);
        }

        // Check if rotating camera
        if (rotateCam)
        {
            // Rotate the camera around the objectToRotate
            rotatingCamera.RotateAround(objectToRotate.position, camRotAxis, rotationSpeed * Time.deltaTime);
            rotatingCamera.LookAt(objectToRotate);
        }
    }
}

让我知道这是否是你试图实现的目标,如果需要,我们可以做一些调整.

Csharp相关问答推荐

System.Data.SQLite:判断SQLite数据库是否为空(任何表中至少有一行)

如何使嵌套for-loop更高效?

在命令行中使用时安装,但在单击时不会安装

为什么我不能更改尚未设置的模拟对象属性的值?

实现List T,为什么LINQ之后它不会返回MyList?<>(无法强制转换WhereListIterator `1类型的对象)'

REST API端点中异步后台代码执行的类型

Blazor Foreach仅渲染最后一种 colored颜色

返回TyedResults.BadRequest<;字符串>;时问题详细信息不起作用

Azure Redis缓存与Entra ID身份验证

自动映射程序在GroupBy之后使用项目

查找表中的模式

JSON空引用异常仅在调试器中忽略try-Catch块,但在其他上下文中捕获得很好

try 链接被委派者(多播委托)时,无法将获取运算符应用于类型为';方法组&39;和方法组';的操作数

如何更改新创建的实例的变量?

为什么Azure函数(独立工作进程)索引失败?使用Azure App配置的CosmosDbTrigger绑定失败,未解析为值

MudBlazor Textfield已禁用,但其验证工作正常

在.NET Maui中,Flyoutindow/Hamburger菜单可以在shell 之外实现吗?

如何在C#控制台应用程序中获取用户输入并将其作为订单项目进行处理?

如何使用.NET 8.0中新的CompositeFormat类?

这是T自身的布尔表达式是什么意思?