检查以下代码问题using SystemCollections;using UnityEngine;public class RhythmClock MonoBehaviour public Transform secondHand; public Transform minuteHand; public Transform hourHand; private double currentTim
以下是代码中存在的问题:
- 在第11行的Awake()方法中,没有给"secondHand"变量赋值。
- 在第20行的Start()方法中,没有检查PlayerController.instance是否为null。
- 在第30行的Update()方法中,使用了未定义的IsStarted变量,应该改为使用isStarted变量。
- 在第35行的if语句中,比较nextTime - currentTime的值是否小于0.0010000000474974513时,没有使用Mathf.Approximately()函数进行浮点数比较。
- 在第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;
}
}
原文地址: https://www.cveoy.top/t/topic/hI8a 著作权归作者所有。请勿转载和采集!