Rigidbody & Velocity: Can you explain this code snippet to me?

Hello,

I’m looking for help to understand a bit of code I got from a tutorial. I know a little programming, but I’m new to Unity and struggling with the physics side. I’ve tried to make a stab at it myself, so please clarify, correct and expand on my (poor) understanding in the simplest way possible. Thanks in advance for your time!

  1. Vector3 constantVelocity = rigidbody.velocity;

Declaring a vector3 to store the velocity of the rigidbody attached to the gameobject this script is attached to. One thing I don’t get about this is how a point in 3d space (Vector 3) can be used to store velocity (speed and direction).

  1. Vector3 tVelocity = constantVelocity.normalized * speed;

Takes the above vector3, normalizes (gives it a magnitude of 1) then multiplies it by the object’s speed. Does this get the next position the gameobject will occupy?

  1. rigidbody.velocity = Vector3.Lerp(constantVelocity, tVelocity, Time.deltaTime * smoothingFactor);

This sets the rigidbody’s velocity. Vector3.Lerp interpolates (guesses) the next position the object will pass through, by taking the start position, end position, and the amount of time.

Also, not sure it’s necessary, but I’ve pasted a chunk of the code in to provide context.

using UnityEngine;
using System.Collections;

public class BallScript : MonoBehaviour 
{
	public float speed;
	public float smoothingFactor;
	
	
	// Use this for initialization
	void Start () 
	{
		// Initialise Vars
		speed = 25.0f;
		smoothingFactor = 30.0f;
		
		// Get the ball rolling
		ApplyStartingForce();	
	}	
	
	
	// Update is called once per frame
	void Update () 
	{
		/** 
		 * !!
		 * 
		 * The next three lines are the bits I'm struggling with 
		 * 
		 * !!
		**/
		Vector3 constantVelocity = rigidbody.velocity;
		Vector3 tVelocity = constantVelocity.normalized * speed;
		rigidbody.velocity = Vector3.Lerp(constantVelocity, tVelocity, Time.deltaTime * smoothingFactor);
		
		// Check if the ball has gone out of bounds
		if(transform.position.x > 21.5f || transform.position.x < -21.5f)
		{
			Debug.Log ("Ball has gone out of bounds");
			transform.position = new Vector3(0, 0, 0);
		}
	}
}

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.

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 :