Check direction of velocity

I am trying to check the direction of velocity.z on another object. Basically, the other object is the player character, and I need to check if the player character is moving backwards and how fast. Based on that, a jumping attack is executed. I am using velocity.z because that is the forward/backward direction of the prefab. This is what I have, the issue is with the velocity.z, I am unsure really what needs to go there but I know what I have is not correct.

// Check Player velocity
    var velocityCheck = GameObject.FindWithTag("Player").transform;
    if (velocityCheck.GetComponent.<Rigidbody>().velocity.z < 0)  // Jumps further if player is moving backwards
        {
        // Jump and apply forward force
        GetComponent.<Rigidbody>().velocity = Vector3 (0, 3, 0);    // Jumps in air
        GetComponent.<Rigidbody>().AddForce (transform.forward * 20, ForceMode.VelocityChange);
        }
  
// Check Player volicity
    if (velocityCheck.GetComponent.<Rigidbody>().velocity.z >= 0)  // Jumps less if player is not moving backwards
    // Jump and apply forward force
    GetComponent.<Rigidbody>().velocity = Vector3 (0, 3, 0);    // Jumps in air
    GetComponent.<Rigidbody>().AddForce (transform.forward * 8, ForceMode.VelocityChange);

“Velocity.z” is not forward relative the the player, it is how fast the player is moving on the world’s z axis. The player’s transform.forward is forward, and you want to see if the velocity is moving in the same direction as the player’s transform.forward.

Use a Vector3.Dot() to find how parallel 2 normalized vectors are. It’ll be something like this:
“Vector3.Dot(player.transform.forward, velocityCheck.GetComponent..velocity.normalized”.

If the vectors were normalized, the Vector3.Dot() will return a value between -1 and 1. 1 means same direction, -1 means opposite directions, and anything in between means different directions.

1 Like

Thanks for the quick reply. I am a little lost on how to use the Vector3.Dot. What would an example if statement look like if you don’t mind?

2 Likes
var player GameObject.FindWithTag("Player");
var playerVelocity = player.GetComponent<RigidBody>().velocity;
var dotProduct = Vector3.Dot(player.transform.forward,playerVelocity);

if (dotProduct < 0)
{
        // DO jumping backward stuff
}

Here is a link on the dot Product:

One of the formula for a dot product between Vector a and Vector b is:
|a| * |b| * cos(theta)
where the |a| means the magnitude of the Vector.
Theta = angle between them if you laid both their start points together at the same point (Imagine 2 vectors at the origin pointing out in different directions).

Now a normalized vector is one whose magnitude = 1. So that formula becomes:
11cos(theta)

If you imagine both Vectors laying directly on top of each other pointing the same direction then theta = 0
Cos(0) = 1. So when 2 Normalized vectors point the same direction their dot product = 1

If you imagine both Vectors facing directly opposite each other, then the angle theta = 180.
Cos(180) = -1.

if they were perpendicular to each other, then theta =90 and Cos(90) = 0.

Vector.transform.forward is always a normalized Vector pointing in the direction the transform is facing.
PlayerVelocity is the velocity vector from the rigidbody and playerVelocity.normalized is the 1 unit magnitude of this vector.

So the dot product between these 2 vectors will always be between -1 and 1. with anything from 0 to -1 meaning the velocity is pointing backwards relative to the forward vector.

2 Likes

That is a really awesome explanation and very helpful to my question! Can’t thank you enough!!

Hey so I am using unityscript, what are the variable types for those 3?

what 3?

The three variables in your example. I am getting errors, and I thought I was assigning the variables incorrectly in unityscript but that is not what is causing the problem I don’t think. I am not at my work computer to post the errors, I will post back soon.

Sorry I pretty much code in c# only so not sure if that is the correct syntax for javascript
player is a GameObject
playerVelocity is a Vector3
dotProduct is a float

thank you very much