Comments and answers for "Rotate Object to Euler Angle using Physics?"
http://answers.unity.com/questions/63667/rotate-object-to-euler-angle-using-physics.html
The latest comments and answers for the question "Rotate Object to Euler Angle using Physics?"Answer by Cameron 3
http://answers.unity.com/answers/64058/view.html
<p>I figured it out, I just used Mathf.Floor to find out if the rotation has reached the right spot, and when setting the rotation... </p>
<p>to set up my rotation:</p>
<pre><code> if (currentState == MoveState.Rotating)
{
float amountToRotate = 180.0f;
float startRotation;
float endRotation;
startRotation = Mathf.Floor(this.rigidbody.rotation.eulerAngles.z);
endRotation = Mathf.Floor(startRotation + amountToRotate);
}
</code></pre>
<p>Then:</p>
<pre><code>void FixedUpdate()
{
Quaternion moveRotation = Quaternion.RotateTowards(Quaternion.Euler(0, 0, Mathf.Floor(this.rigidbody.rotation.eulerAngles.z)), Quaternion.Euler(0, 0, endRotation), Time.deltaTime * RotateSpeed);
this.rigidbody.MoveRotation(moveRotation);
if (Mathf.Floor(this.rigidbody.rotation.eulerAngles.z) == Mathf.Floor(moveRotation.eulerAngles.z))
{
this.rigidbody.rotation = Quaternion.Euler(0, 0, endRotation);
currentState = MoveState.DoneRotating;
}
}
</code></pre>Thu, 12 May 2011 06:46:12 GMTCameron 3Answer by Cameron 3
http://answers.unity.com/answers/63962/view.html
<p>Yes, rigidbody.MoveRotation seemed to be the way to go. I had to re-write stuff to perform the rotation in FixedUpdate, but that wasn't a big deal. </p>
<p>I do have one more question related to this. For some reason, using a Euler angle of 180.0f makes the object rotate clockwise properly a few times, but then it starts bouncing back and forth beween 180 degrees and 6.465943e-05 (basically zero, even tho I'm telling Quaternion.Euler to use 0.0f). So it starts moving clockwise, then bounces between clockwise and counter-clockwise. I'm using an orthographic camera on an X/Y plane, so +Z represents rotation right, and -Z represents rotation left.</p>
<p>I changed my RotateLerp function to the following:</p>
<pre><code>IEnumerator RotateLerp(Vector3 StartPosEuler, Vector3 EndPosEuler)
{
endRotation = Quaternion.identity;
endRotation.eulerAngles = EndPosEuler;
Debug.Log("Starting Rotation: " + StartPosEuler + " - End: " + EndPosEuler.z + " EndRotation: " + endRotation.eulerAngles);
while (Quaternion.Angle(this.rigidbody.rotation, endRotation) > 0.1f)
{
yield return true;
}
}
</code></pre>
<p>So basically a CoRoutine starts which takes the object's current rotation euler angles, and another vector3 which is just that euler angles vector with +180.0f on the Z-angle. Then it builds the endRotation quaternion which is a global of type Quaternion that FixedUpdate works with. I tried building it two ways, using the method above, where you initialize to identity and just set the euler angles, and I also tried using Quaternion.Eulers(EndPosEuler). The CoRoutine waits in a while loop until the rotation is "effectively" done, and then sets the state of the rotation object to MoveState.Waiting so that FixedUpdate stops rotating it. </p>
<p>Then in my Fixedupdate, I check the state of the object and if it is supposed to be moving, it uses this.rigidbody.MoveRotation to move the object:</p>
<pre><code>if (currentState == MoveState.Moving)
{
float Angle = Quaternion.Angle(this.rigidbody.rotation, endRotation);
Debug.Log("Angle: " + Angle);
Quaternion moveRotation = Quaternion.RotateTowards(this.rigidbody.rotation, endRotation, Time.deltaTime * (RotateSpeed * RotationPoints[CurrentRotationPoint].SpeedMultiplier));
this.rigidbody.MoveRotation(moveRotation);
}
</code></pre>
<p>So what happens is, the object rotates 180 degrees clockwise, then another 180 degrees clockwise, then another, so it's going in a circle... But eventually it starts going 180 degrees clockwise, then 180 degrees COUNTER-clockwise, and bounces back and forth between those two points forever. </p>
<p>My thoughts are it could be one of these things:</p>
<p>1) The while loop with my Quaternion.Angle check, but I don't know a good way to test that it's reached a specific euler angle since it doesn't appear to be an exact science. I can't test for "0.0f", for example, because Unity is setting the Z euler to 6.465943e-05, etc. An equality comparison didn't work the first time I tried it.</p>
<p>2) Quaternion.RotateTowards is doing some kind of rounding/optimization with the two rotations I am giving it, and not producing exact euler angles even tho both rotations have exact euler angles on them... This seems to be the case since it's ending up with 6.465943e-05 instead of zero.</p>
<p>Another interesting effect is that if I use 90 degree turns, or 45 degree turns, the object continues to rotate clockwise just fine. This seems to only be a problem when I do 180.0f. </p>
<p>Also, if I do 180.0001f, it works fine and turns clockwise. </p>
<p>Any light that can be shed on this will be very much appreciated :) And it will make an awesome Unity answer too! :) :)</p>
<p>Thanks!</p>Wed, 11 May 2011 16:59:00 GMTCameron 3Answer by Senhor de todo o Mal
http://answers.unity.com/answers/63692/view.html
<p>Instead of transform.localRotation have you tried <a href="http://unity3d.com/support/documentation/ScriptReference/Rigidbody.MoveRotation.html" rel="nofollow">rigidbody.MoveRotation</a> or <a href="http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddTorque.html" rel="nofollow">rigidbody.AddTorque</a>?</p>Tue, 10 May 2011 11:00:09 GMTSenhor de todo o Mal