gpt4 book ai didi

c# - 在协程中使用 Quaternion.RotateTowards() 旋转对象?

转载 作者:行者123 更新时间:2023-11-30 23:08:44 26 4
gpt4 key购买 nike

我正在尝试使用 Quaternion.RotateTowards()Coroutine 中旋转一个对象。但它不像在 Update() 中那样顺畅地旋转。

相反,它捕捉到目标旋转。

怎么了?这是我的脚本:

public class SpinWheelScript : MonoBehaviour {

public GameObject[] rewards;
public GameObject[] pointerDir;
public GameObject[] ringDir;
public GameObject pointer;
public GameObject ring;

private Quaternion pointerTargetRotation;
private Quaternion ringTargetRotation;

private bool spinIsEnabled = true;
private bool isSpinning = false;

private float pSpeed;
private float rSpeed;
private float pointerRotateFloat;
private float ringRotateFloat;

private int rewardEnabler = 0;
private int randomReward;
private int pRandomDir;
private int rRandomDir;
private int plastRandomDir = 0;
private int rlastRandomDir = 0;

private void Update()
{
if (spinIsEnabled == false)
{
// Do Nothing
}

else if (spinIsEnabled == true && isSpinning == false)
{
// Select reward
if (rewardEnabler == 0)
{
RewardSelector();
rewardEnabler = 1;
}
StartCoroutine(Spin());
}
}


IEnumerator Spin()
{
isSpinning = true;
pSpeed = 0;
rSpeed = 0;

// Speed increasing spin for 5 seconds

PointerRandomDirection();
RingRandomDirection();

while (true)
{
pointerRotateFloat = (((pRandomDir + 1) * 60) - 30) - pointer.transform.rotation.z;
ringRotateFloat = (((rRandomDir + 1) * 60) - 30) - ring.transform.rotation.z;

pSpeed = (pointerRotateFloat / 2);
rSpeed = (ringRotateFloat / 2);

pointerTargetRotation = Quaternion.Euler(new Vector3(0, 0, pointerRotateFloat));
pointer.transform.rotation = Quaternion.RotateTowards(pointer.transform.rotation, pointerTargetRotation, pSpeed * Time.deltaTime);
ringTargetRotation = Quaternion.Euler(new Vector3(0, 0, ringRotateFloat));
ring.transform.rotation = Quaternion.RotateTowards(ring.transform.rotation, ringTargetRotation, rSpeed * Time.deltaTime);

if ((pointer.transform.rotation == pointerTargetRotation) && (ring.transform.rotation == ringTargetRotation))
break;
}

yield return new WaitForSeconds(1);

Debug.Log("Done!");
}

private int RewardSelector()
{
randomReward = Random.Range(0, rewards.Length);

return randomReward;
}

private int PointerRandomDirection()
{
int pRandomDir = plastRandomDir;

if (pointerDir.Length <= 1)
return 0;

while (pRandomDir == plastRandomDir)
{
pRandomDir = Random.Range(0, pointerDir.Length);
}

plastRandomDir = pRandomDir;
return pRandomDir;
}

private int RingRandomDirection()
{
int rRandomDir = rlastRandomDir;

if (ringDir.Length <= 1)
return 0;

while (rRandomDir == rlastRandomDir)
{
rRandomDir = Random.Range(0, ringDir.Length);
}

rlastRandomDir = rRandomDir;
return rRandomDir;
}
}

最佳答案

您没有在协程内生成等待指令。

while (true) 循环的内部(可能在末尾),只需编写类似 yield return null; 的内容。这将告诉协程将下一行代码的执行延迟到下一帧。

关于c# - 在协程中使用 Quaternion.RotateTowards() 旋转对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46316488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com