Answers for "Vector3 precision issue?"
http://answers.unity.com/questions/18634/vector3-precision-issue.html
The latest answers for the question "Vector3 precision issue?"Answer 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 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