触发Oculus上的旋转转到C#统一

时间:2019-03-08 19:48:13

标签: c# unity3d rotation coroutine oculus

我希望按下oculus go上的触发器时旋转游戏对象,但是只有这样,我才有以下代码。不幸的是,即使我将手指从扳机上移开,这也会持续旋转

public GameObject dummyrotate;
private bool rotate = false;
float rotationAmount = .3f;
float delaySpeed = .1f;


public void rotateAntiClockwise()

{
    StartCoroutine(SlowSpin2());
}

public IEnumerator SlowSpin2()

{
    float count = 0;
    while (count <= 90)

    {
        dummyrotate.transform.Rotate(new Vector3(0,-rotationAmount,0));
        count += rotationAmount;
        yield return new WaitForSeconds(delaySpeed);
    }
}

非常感谢 乔诺

1 个答案:

答案 0 :(得分:0)

好吧,问题是一旦您触发旋转,由于您的while循环,将需要30秒才能完成coroutine。而且,当再次触发它时,您将启动另一个coroutine,它将像这样永远持续下去。我假设每次按下触发器时都会调用rotateAntiClockwise方法。

我还假设您想将对象沿y轴旋转90度,您可以这样实现:

 bool isRunning = false;
 //Make sure this variable is true when triggered and false when not
 bool isTrigger = false;
 float rotationAmount = 0.3f;
 public IEnumerator SlowSpin2()
 {
    isRunning = true;
    //This will rotate the object continuously until isTriggered is false
    while (isTriggered)
    {
        Debug.Log("Rotating");
        transform.Rotate(new Vector3(0, -rotationAmount, 0));            
        yield return null;

    }
    Debug.Log(Time.time);
    isRunning = false;
 }

此脚本将在2秒内将对象从0缓慢旋转到90。

然后,您必须像这样修改rotateAntiClockwise,以便在该协程已经运行时不会启动另一个协程。

public void rotateAntiClockwise()
{
     if(!isRunning)
         StartCoroutine(SlowSpin2());
}