Download

Get the distance to be traveled based on velocity

Hi all,

this might be a very stupid question, but after searching for a while I couldn’t find and answer around here.
I’m using the default CharacterMovement to control the player and I want to build up some very custom vector to move him towards, when the user goes right/left with the controller’s stick.

Because I want for my character to move along a given trajectory (imagine a sine wave, or a circle or whatever) I want for it to move on the point of such trajectory at a given distance, that must be proportional to its velocity.
Basically what I’d like to do when the input triggers this, is to compute the distance the player will travel in that span of time, remap such distance onto the trajectory function, get back the vector pointing from the player to such point and pass it to CharacterMovement.
In order to do so, of course, I need the distance but I’m having hard times believing that what I’m getting is the correct value.
What I’m trying to do is


distance=getVelocity*deltaTime.

I’m getting the velocity value as magnitude of the vector pulled from CharacterMovement and I multiply this by GetWorldDeltaSeconds.

First of all: does this make sense or there’s better ways of doing so?
Second: in what units is velocity computed?

Thanks everyone for the answers.

Cheers,
e

Well… at this point I guess it’s quite tricky?

They should probably make a math section of the forums… :stuck_out_tongue:

It really all depends on what units you are using. And this can be a bit tricky math wise. First of all, distance and velocity will need to be using the same units (meters, feet, mm, etc…) and the velocity can’t change with that equation.

It would be correct, and a great method to use ’ distance = getVelocity*deltaTime ', but velocity must remain a constant.

If you start to get into dynamic accelerations, then it can get VERY complicated and confusing. If you use dynamic accelerations, then I suggest you either just use an average acceleration, or get an average acceleration every second, and calculate distance every second, and then add them all up. This dynamic acceleration method isn’t very accurate, but to avoid confusion; feel free to sample very millisecond for more accuracy.

The (default) unreal unit is the centimeter (cm) so the velocity magnitude is cm/s.

If the velocity is 500 for example then the pawn will travel 500 cm (5 meters) in the next second, assuming the velocity will not change of course.

Then of course you multiply that by deltaTime if you need the distance per frame :stuck_out_tongue:
So your math look pretty correct to me?

Thanks everyone, that makes sense.
More questions then… :slight_smile:

TK-Master, that assumes velocity is just an attribute of the CharacterMovement component, not the actual speed of the pawn at a given time, is it?
Sounds like I’d need the derivative of velocity (acceleration) in order to be able to tell what’s the predicted velocity at the about-to-finish tick.
This would mean, at every tick (or second?) storing the current velocity, read it back at the next tick/second, compute the acceleration based on those, find the distance based on the projected velocity I just found.

Correct?
Or there’s an already in-built method/function to get acceleration and/or distance the pawn will travel during the current tick?

Tanks again, I guess most of this issues comes from my poor knowledge of the unreal engine itself…:confused:

Get velocity doesn’t account for Z velocity(It just says 0) when in walking movementmode so you won’t have accurate results using it. Get Physics Linear Velocity (Capsule Component) on the other hand will give you X,Y and Z.

My math may be wrong here but I would do (New location - Old Location) Vector Length*0.01, that should give you distance in metres.

Get Physics Linear Velocity – Vector Length*0.01 will give you Metres per second, 0.036 will give you KM/H

Something like this?
Image 1.png

I tried that, but whether I move or I stay still, that always returns zero…

It needs an input, something like Event Tick

Right!

I’ll give it a go, cheers