- Home /
Duplicate Question
https://answers.unity.com/questions/1469920/ienumerator-quaternion-only-rotating-once.html
ienumerator quaternion only rotating once
This script is for a overhead spotlight that is a child of a gameobject that is suppose to stimulate a searchlight. The rotation works perfectly the first time but, it will not rotate thereafter. i have a feeling it might have something to do with the "random.range" part pulling from the same seed value but other than that im out of ideas.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpotlightController : MonoBehaviour {
private IEnumerator Start()
{
while (Application.isPlaying) {
Quaternion randomRot = Quaternion.Euler (Random.Range (-10, 10), Random.Range (-20, 20), 0f);
while(Vector3.Distance(this.transform.root.localEulerAngles, randomRot.eulerAngles) > .1f)
{
this.transform.localRotation = Quaternion.Lerp(this.transform.localRotation, randomRot, Time.deltaTime);
yield return null;
}
yield return new WaitForSeconds(1f);
}
}
Enter debug statements before the inner while loop to check the values.
e.g.
Debug.Log("Rotation " + randomRot);
Debug.Log("Distance " + Vector3.Distance(this.transform.root.localEulerAngles, randomRot.eulerAngles));
Does the Distance value get stuck to
@dowens9021 do you realize that by posting duplicate questions you are actually wasting the valuable time of many forum members who voluntarily engaged with your question (just read and/or responded) to help you?
Answer by ElijahShadbolt · Feb 17, 2018 at 11:47 PM
It probably has something to do with using Euler angles, because Vector3.Distance does not work for rotations.
Replace
while(Vector3.Distance(this.transform.root.localEulerAngles, randomRot.eulerAngles) > .1f)
with
while(Quaternion.Angle(this.transform.localRotation, randomRot) > 1.0f)
Well, Distance can work with eulerAngles given the threshold is large enough. However since euler angles might not have a unique representation of a particular rotation.
Though the main error seems to be that he checks the rotation of the root object and not the actual object. He rotates transform.localRotation
but he checks transform.root.localEulerAngles
which doesn't make much sense. In general transform.root.localEulerAngles
is the same as transform.root.eulerAngles
since the root can't have a parent and therefore its local angles are its worldspace angles.
Follow this Question
Related Questions
ienumerator quaternion only rotating once 1 Answer
Flip over an object (smooth transition) 3 Answers
Quaternions acting up 1 Answer
How to make a plane face a static but rotating camera? 3 Answers
Why is this rotation acting odd? 0 Answers