问题是当运行游戏时,当标志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
现在我判断了国旗和相机不再旋转了,立方体是围绕着自己旋转.
现在摄像机也面向天空就像是从底部看立方体一样.我希望它能像一开始时相机绕着立方体旋转的样子.