How is the velocity from the "Get Velocity" node calculated?

I’m trying to create my own “character movement” component. So I started from the “Pawn” class, and created an actor component from scratch and started to program it in blueprints. Everything was going fine until I needed to get the velocity of the character, so I used the “Get Velocity” node and plugged it into a print string just to check. To my surprise, the actor velocity was always 0, 0 ,0, even when the pawn was clearly moving.

So I searched the documentation and it says that the Get Velocity node “Returns velocity of the rootcomponent if it is either using physics or has an associated MovementComponent”. My pawn is not using physics and it doesn’t have a MovementComponent (except for my own custom movement component that I’m trying to create from scratch), so I assume that’s the reason.

So, is there a way to make my own movement component also contribute to the calculation of the actor velocity that is given when you use the Get Velocity node?

By the way, the way I’m applying the movement is I get the pending movement input and use it to calculate the resulting movement and plug it into a “set actor location” node with sweep enabled. And after that I use the “consume movement input”:

1 Like

Hi @AleMarcati :slight_smile:
I love seeing you create your own movement component for the character. Good job!
Now you are responsible for calculating the velocity for your character :smile: . To do this, simply save the old location before updating your character location, get the new location of your character after the update, calculate the delta movement and divide it by DeltaTime, now set your velocity with the newly calculated speed value.

2 Likes

Btw. if you use the GetVelocity function of your pawn, then the velocity value depends on your setup.
If you have a root component that simulates physics then you will get the velocity from that component, otherwise the velocity will come from your movement component if there is one.

2 Likes

But what I understand is that the built-in character movement component contributes to the value of the ‘velocity’ of the character actor, so you can get the velocity directly from the character even if it’s set by the movement component.
I get that in my custom movement component I’ll have to calculate the velocity on my own, but is there a way I can set the velocity value of the actor so I can still use the “get velocity” function (like if I want to get the character velocity from another actor BP without having to cast to the component)?

1 Like

Mhm yeah, that’s basically what I wrote ^^.
If you use your pawn’s GetVelocity function, it will be taken from your movement component, unless your root component has Simulate Physics enabled.

1 Like

But how do I make my custom movement component work like the built-in movement component and make the value from the ‘get velocity’ reflect the movement from my own custom movement component?
Is there a way to do that?

The GetVelocity function is virtual and can be overridden in C++, I don’t see a way this could be done with Blueprints other than deriving from UMovementComponent or a subclass

The default behavior of GetVelocity for the pawn is
do I have a root component and does it simulate physics?
if yes then return the velocity of that component.
If not do I have a movement component?
If yes then return the velocity of the movement component.

GetVelocity of your pawn automatically reflects the velocity value of your movement component.

Ohhhh your custom movement component isn’t a movement component. Okay :sweat_smile:
Yeah, that won’t work then.

Yes, I was trying to create it from scratch. I realize now it’s way above my current skill level and knowledge.
Thanks for your answers anyway, they helped me understand some things.

1 Like