以下是代码中存在的问题:

  1. 在第 11 行的 Awake() 方法中,没有给'secondHand' 变量赋值。
  2. 在第 20 行的 Start() 方法中,没有检查 PlayerController.instance 是否为 null。
  3. 在第 30 行的 Update() 方法中,使用了未定义的 IsStarted 变量,应该改为使用 isStarted 变量。
  4. 在第 35 行的 if 语句中,比较 nextTime - currentTime 的值是否小于 0.0010000000474974513 时,没有使用 Mathf.Approximately() 函数进行浮点数比较。
  5. 在第 38 行的 StartCoroutine() 方法中,传递了一个 IEnumerator 类型的变量 moveCoroutine,但是 moveCoroutine 并没有在代码中定义。

修复后的代码如下:

using System.Collections;
using UnityEngine;

public class RhythmClock : MonoBehaviour
{
    public Transform secondHand;
    public Transform minuteHand;
    public Transform hourHand;

    private double currentTime;
    private double nextTime;
    private bool isStarted;

    private const float interval = 0.5263158f;

    private float rotHour;
    private float rotMin;

    private void Awake()
    {
        int num = Random.Range(0, 180);
        float num2 = -15f;
        float num3 = num2 / 12f;
        rotMin = num2 * (float)num;
        rotHour = num3 * (float)num;
        if (secondHand != null)
        {
            secondHand.rotation = Quaternion.Euler(0f, Random.Range(0, 360), 0f);
        }
    }

    private void Start()
    {
        if (PlayerController.instance != null)
        {
            PlayerController.instance.OnStart += Instance_OnStart;
        }
    }

    private void Instance_OnStart()
    {
        currentTime = AudioSettings.dspTime;
        nextTime = currentTime + 0.5263158082962036;
        isStarted = true;
    }

    private void Update()
    {
        if (isStarted)
        {
            secondHand.Rotate(0f, -360f * Time.deltaTime, 0f);
            currentTime = AudioSettings.dspTime;
            currentTime += Time.deltaTime / Time.timeScale;
            if (Mathf.Approximately((float)(nextTime - currentTime), 0.0010000000474974513))
            {
                StartCoroutine(MoveFunc());
                nextTime += 0.5263158082962036;
            }
        }
    }

    private IEnumerator MoveFunc()
    {
        float t = 0f;
        float targetRotMin = rotMin - 20f;
        float targetRotHour = rotHour - 2.5f;
        while (t < 0.3f)
        {
            rotMin = Mathf.Lerp(rotMin, targetRotMin, Time.deltaTime * 10f);
            rotHour = Mathf.Lerp(rotHour, targetRotHour, Time.deltaTime * 3f);
            if (minuteHand != null)
            {
                minuteHand.rotation = Quaternion.Euler(0f, rotMin, 0f);
            }
            if (hourHand != null)
            {
                hourHand.rotation = Quaternion.Euler(0f, rotHour, 0f);
            }
            t += Time.deltaTime / Time.timeScale;
            yield return null;
        }
    }
}

在修复后的代码中,我们对代码进行了如下修改:

  1. 在 Awake() 方法中,添加了对 secondHand 的 null 检查,并在其不为 null 时才进行赋值操作。
  2. 在 Start() 方法中,添加了对 PlayerController.instance 的 null 检查,并在其不为 null 时才进行事件监听。
  3. 在 Update() 方法中,将 IsStarted 变量更正为 isStarted。
  4. 在 Update() 方法中的 if 语句中,使用 Mathf.Approximately() 函数进行浮点数比较,以避免由于精度问题导致的错误。
  5. 在 MoveFunc() 方法中,添加了对 minuteHand 和 hourHand 的 null 检查,并在其不为 null 时才进行赋值操作。

这些修改可以有效地解决代码中存在的问题,并提高代码的稳定性和可靠性。

Unity RhythmClock 脚本代码问题修复及优化

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

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