以下是一个使用空格键控制物体旋转的 Unity 脚本示例:

using UnityEngine;

public class ObjectRotation : MonoBehaviour
{
    public float rotationSpeed = 10f;
    private bool isRotating = false;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            isRotating = !isRotating;
        }

        if (isRotating)
        {
            // 获取当前物体的旋转角度
            Vector3 currentRotation = transform.rotation.eulerAngles;

            // 更新旋转角度
            currentRotation.y += rotationSpeed * Time.deltaTime;

            // 应用新的旋转角度
            transform.rotation = Quaternion.Euler(currentRotation);
        }
    }
}

将上述代码添加到一个空的 GameObject 上,并将其作为脚本的组件。在 Unity 编辑器中,您可以调整 'rotationSpeed' 变量的值来控制旋转速度。当您按下空格键时,物体将开始旋转,再次按下空格键时,物体将停止旋转。

这是一个基本示例,您可以根据您的需求进行修改和扩展。希望对您有所帮助!

Unity 脚本:使用空格键控制物体旋转

原文地址: https://www.cveoy.top/t/topic/bQ06 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录