触发 oculus go c# unity 上的旋转
roation on trigger oculus go c# unity
我希望在按下 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);
}
}
非常感谢
乔诺
好吧,问题是一旦你触发旋转,由于你的 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());
}
如果您只是想让它在按住触发器时旋转,请不要为此使用协程。协程将 运行 直到它们完全完成,所以请尝试这样的事情:
public GameObject dummyrotate;
private bool rotate = false;
float rotationAmount = .3f;
float delaySpeed = .1f;
void Update()
{
SlowSpin2();
}
public void SlowSpin2()
{
if (rotate == true) {
dummyrotate.transform.Rotate(new Vector3(0,-rotationAmount,0));
count += rotationAmount;
}
}
我希望在按下 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);
}
}
非常感谢 乔诺
好吧,问题是一旦你触发旋转,由于你的 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());
}
如果您只是想让它在按住触发器时旋转,请不要为此使用协程。协程将 运行 直到它们完全完成,所以请尝试这样的事情:
public GameObject dummyrotate;
private bool rotate = false;
float rotationAmount = .3f;
float delaySpeed = .1f;
void Update()
{
SlowSpin2();
}
public void SlowSpin2()
{
if (rotate == true) {
dummyrotate.transform.Rotate(new Vector3(0,-rotationAmount,0));
count += rotationAmount;
}
}