Calculating speed of actor relative to player

Hello there!

I am very sorry to come up with that noobish question, but I am kind of lost with the concept of vector math …

I want to calculate if an actor moves towards or away from the player and its relative speed, as I want to use this for simulating a doppler effect. I am aware that there is a doppler node in the sound cues itself, but I need to map this to a variable that would be driving a parameter in an audio middleware instead, ideally ranging from -1 (away from player at max. speed) to 1 (towards player at max speed).

Can anybody point me to the right direction or resources helping me to undestand how this could be achieved in blueprints?

Thanks a lot!

Cheers

Felix

Wouldn’t it simply be:

Speed of actor - speed of player?

Blueprints have a GetVelocity node that you can use to get the world space velocity vector.

Hi,

Thanks a lot for the reply!

So I have come up with a simple blueprint like that, but this provides me just with the relative speed without a direction, like is the actor moving towards the player or away.
Furthermore I realise that my description of the problem was not 100% accurate: I am trying to get the relative speed of actor and player if their distance to each other is changing - with the above set up the actor could for instance move in a fixed circle around the player but the variable with the vector length would still be different from 0, as the actor moves with a different absolute speed than the player.

Or in other words:
I am trying to translate the movement of an actor towards the player or away from it and also the speed of this into a variable ranging from -1 (actor @ max. speed from player) to 1 (actor @ max. speed towards player) with the value matching the speed and a value of 0 indicating the actor moving neither away from nor towards the player. Phew :slight_smile:

Like I said, this whole vector math is a bit above my head currently and I am struggeling with finding the proper resources to translate the above problem into a blueprint.

Cheers

Felix

The dot product of the normalized (normalize node) velocity vector and the normalized difference in position vector should do it, -1 to 1. Right?

The dot product of both velocities would give you a value between -1 and 1, yes. I don’t think it really matters if they are normalized or not. You could then run a check to see if the dot product is less than 0 and multiply your relative velocity by -1 if it is.

There is a node that does this all for you called “get dot product to” (there is also a get horizontal dot product to that ignores the z axis) and all you need to input are the two actors. The top pin is the actor that you want to look through the “eyes” of and the second pin is the target.

So going based on what you asked, in the first post, I’m assuming that you want the non-player character to be the one you need to look through the eyes of and the player character becomes the target. If the other actor is looking at the player, it will return 1.0, if it’s turned around 180 degrees, it will return -1.0, if it’s turned 90 degrees, it will return 0 and if it’s looking toward the player with a 45 degree angle, it will return 0.707, etc etc. You can then take the actor’s velocity and multiply it by that dot product. This will yield the percentage of the velocity that is heading toward the player.

Using the “looking at the player with a 45 degree angle = 0.707” example: Let’s say the actor is travelling at 600 units per second. This would mean that the actor would be heading toward the player at a rate of 424.2 units per second.

@IronicParadox, thanks. I always forget about the more specialized nodes and end up reinventing the wheel.