我刚开始我的游戏开发之旅,昨天刚开始学习课程,课程来自gamedev.tv(但是在udemy)。目前的经验还算顺利,但最近遇到一个问题,问题出在我添加旋转动作后,火箭的速度会突然加快,无法正常工作。由于我太新手了,我自己做了研究,但找到的答案几乎没有。因此,任何帮助都会大大地被感激。

using UnityEngine;
using UnityEngine.InputSystem;
public class Movement : MonoBehaviour
{
    // 声明输入动作
    [SerializeField] InputAction thrust;
    [SerializeField] InputAction rotation;
    // 旋转和推进的强度
    [SerializeField] float rotationStrength = 0f;
    [SerializeField] float thruststrength = 0f;

    // Rigidbody
    Rigidbody rb;

    void Start()
    {
        // 获取Rigidbody组件
        rb = GetComponent<Rigidbody>();
    }

    // 启用输入动作
    private void OnEnable()
    {
        // 启用推进和旋转输入动作
        thrust.Enable();
        rotation.Enable();
    }

    // 每帧固定更新
    private void FixedUpdate()
    {
        // 处理推进
        ProcessThrust();
        // 处理旋转
        ProcessRotation();
    }

    // 处理推进
    private void ProcessThrust()
    {
        // 检查是否按下推进按钮
        if (thrust.IsPressed())
        {
            // 添加相对力
            rb.AddRelativeForce(Vector3.up * thruststrength * Time.fixedDeltaTime);
        }
    }

    // 处理旋转
    private void ProcessRotation()
    {
        // 读取旋转输入
        float rotationInput = rotation.ReadValue<float>();
        // 检查输入方向
        if (rotationInput < 0)
        {
            // 左侧旋转
            ApplyRotation(rotationStrength);
        }
        else if (rotationInput > 0)
        {
            // 右侧旋转
            ApplyRotation(-rotationStrength);
        }
    }

    // 应用旋转
    private void ApplyRotation(float rotationThisFrame)
    {
        // 旋转
        transform.Rotate(Vector3.forward * rotationThisFrame * Time.fixedDeltaTime);
    }
}