Comments and answers for "Vector3 precision issue?"
http://answers.unity.com/questions/18634/vector3-precision-issue.html
The latest comments and answers for the question "Vector3 precision issue?"Comment by Cap on Cap's answer
http://answers.unity.com/comments/73683/view.html
Well, I still have a long way to go when it comes to the mathematical side of things. I'm sure I'll be in at the deep end when it comes to writing the AI for all the other ships flying around; that'll probably bring some clarity :)Thu, 03 Jun 2010 00:17:45 GMTCapComment by Eric5h5 on Eric5h5's answer
http://answers.unity.com/comments/73681/view.html
@Cap: Actually you don't have to understand quaternions to understand that code, which is good because otherwise I couldn't have written it. ;) Basically it's saying "I want a rotation that's the opposite of my velocity. This angle is the difference between my current rotation and the wanted rotation. Now interpolate between my current rotation and the wanted rotation, where the amount of interpolation this frame depends on the angle." The quaternions are kind of a black box; as long as you understand lerp, then the quaternions don't really matter.Wed, 02 Jun 2010 23:27:57 GMTEric5h5Comment by Cap on Cap's answer
http://answers.unity.com/comments/73680/view.html
Well I still haven't got my head around Quaternions so I have no idea why or how your code snippet works. It's almost certainly more efficient than mine tho, so I'll use it anyway. Thanks :)Wed, 02 Jun 2010 23:12:36 GMTCapComment by Eric5h5 on Eric5h5's answer
http://answers.unity.com/comments/73679/view.html
You definitely can use Slerp; see the edit to my answer for what I meant.Wed, 02 Jun 2010 22:43:46 GMTEric5h5Answer by Cap
http://answers.unity.com/answers/18687/view.html
<p>Found a solution that works for me. This lets me turn the ship using its rotation speed value measured over time, letting go of the key stops the rotation, and it picks the shortest route when deciding which way to turn. The 2 variance was necessary to overcome the thing skipping right over 180 but it's more than accurate enough.</p>
<pre><code>playerdir=transform.rigidbody.velocity; // record vector of movement
if(Input.GetAxis("Vertical")<0) { // reverse
if(Mathf.RoundToInt(Vector3.Angle(playerdir,transform.forward))<178) { // 2 variance
rotation = rotationspeed*Time.deltaTime;
var angle1 = Vector3.Angle(transform.right, playerdir);
var angle2 = Vector3.Angle(-transform.right, playerdir);
if(angle1>angle2) { // find shortest route left/right
transform.Rotate (0,rotation,0);
} else {
transform.Rotate (0,0-rotation,0);
}
}
}
</code></pre>Wed, 02 Jun 2010 21:01:22 GMTCapComment by Cap on Cap's answer
http://answers.unity.com/comments/73675/view.html
I really don't think I can use slerp for this, but I managed to stumble around until I found a solution that works reliably, if not particularly efficiently. There's a buffer of 2 but that's fine for my needs. Gonna stick it in an answer below.Wed, 02 Jun 2010 20:57:33 GMTCapComment by Eric5h5 on Eric5h5's answer
http://answers.unity.com/comments/73660/view.html
@Cap: You can use `Quaternion.Angle(wantedRotation, transform.rotation)` to get the difference in degrees between the current rotation and the desired rotation, and then use that in Quaternion.Slerp to rotate by a constant rate.Wed, 02 Jun 2010 18:07:15 GMTEric5h5Comment by Cap on Cap's answer
http://answers.unity.com/comments/73640/view.html
Thankyou, that certainly flips the ship around to face the right way, but it does it instantly. How can I match the speed of this rotation to my rotationspeed variable, based on time? That's why I was trying to do it the way in the code above, because my object has a set rotation speed and the user can let go of the key at any time and the rotation should stop wherever it is. Slerp doesn't seem appropriate for that.Wed, 02 Jun 2010 10:52:00 GMTCapAnswer by Eric5h5
http://answers.unity.com/answers/18636/view.html
<p>Print the individual X/Y/Z elements of the Vector3 to see what they are without the rounding you get when printing the entire thing. It's almost impossible that the transform.forward would exactly equal the Vector3 that you're comparing it to; try using a range instead.</p>
<p>However, it doesn't seem like that would actually work anyway; if you use</p>
<pre><code>var wantedRotation = Quaternion.LookRotation(-rigidbody.velocity);
</code></pre>
<p>then that would give you a rotation of the opposite direction of movement, which you could then rotate towards with Quaternion.Slerp.</p>
<p>Edit: like this...</p>
<pre><code>if (Input.GetAxis("Vertical") < 0.0) {
var wantedRotation = Quaternion.LookRotation(-rigidbody.velocity);
var angle = Quaternion.Angle(wantedRotation, transform.rotation);
var t = (1.0/angle) * Time.deltaTime * rotationSpeed;
transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, t);
}
</code></pre>Wed, 02 Jun 2010 09:20:07 GMTEric5h5