Answers for "Rigidbody & Velocity: Can you explain this code snippet to me?"
http://answers.unity.com/questions/348782/rigidbody-velocity-can-you-explain-this-code-snipp.html
The latest answers for the question "Rigidbody & Velocity: Can you explain this code snippet to me?"Answer by jedy
http://answers.unity.com/answers/348938/view.html
Okay,
First **Vectors** :
Vectors are finite lines with direction and length.
Basically if you need to direct something it takes just a direction vector. The vector starts at the basis of the coordinate system ( usually a Point at [x = 0, y = 0, z = 0] or [0,0,0] ). The magnitude ( length ) of the vector is commonly used for the speed or some other property to accompany the direction vector.
**Magnitude** - the magnitude is simply calculated by the Pythagorean theorem ( magnitude = sqrt(x^2 + y^2 + z^2 ).
**Normalization** ( someVector.normalized ) reduces the magnitude of the vector to 1.
**Vector3.Lerp** :
Lerp is a function for Linear Interpolation. It smoothly interpolates a value from point a to point b.
In our scenario it interpolates from constantVelocity to tVelocity over Time.deltaTime * smoothingFactor.
Okay to the the **point** with an overdue:
One thing you should notice is that the variable named **constantVelocity** is actually a variable describing the **current** velocity, and the variable **tVelocity** is short for **target velocity**.
So basically the three lines you care about cache the current velcoity.
Than they normalize the currentVelocity to get its magnitude to one ( this is used to capture just the direction of the vector without the magnitude in the way ), than multiplying it by the speed, effectively producing a vector which points in the same direction, but with different speed ( we're using the magnitude to represent that ).
Finally we're interpolating between the current velocity and the target velocity ( with full speed ) over deltaTime multiplied by some smoothing factor, to make it look slicker. We than simply assign the new interpolated value to the current velocity.
Additional information :
http://en.wikipedia.org/wiki/Euclidean_space
http://en.wikipedia.org/wiki/Coordinate_vector
http://en.wikipedia.org/wiki/Pythagorean_theorem
http://en.wikipedia.org/wiki/Lerp_%28computing%29Thu, 15 Nov 2012 23:15:29 GMTjedyAnswer by sirival
http://answers.unity.com/answers/348926/view.html
First of all a Vector3 is not just a point in space. It is a struct that contains 3 values. A vector can be a point in space but it can also be a direction or a velocity etc.
When you normalize a velocity and then multiply it with a number you essentially Clamp the magnitude of that velocity to the number that you multiplied it with (speed in your case).
So what this script does at the point where you are struggling is take the current velocity of the rigid body, create a desired velocity and then smoothly interpolate towards that desired velocity over time.Thu, 15 Nov 2012 22:29:43 GMTsirival