#having problems with velocity

9 messages · Page 1 of 1 (latest)

eager blade
#

so I'm trying to write a character control semi similar to taitan fall 2's and apex legends
where you gain velocity from different things (like an exploding grenade or something) while also having the velocity you add via inputs (wasd)
basically what I'm am trying to achieve is something like

private void FixedUpdate(){
rb.velocity += input;
}

obviously the problem with the code above is if you keep holding forward key for n frames, your velocity will become n time the speed limit

one fix (which didn't work) that I came up with was

private void FixedUpdate(){
rb.velocity -= input_from_last_frame
rb.velocity += input;
}

but this doesn't work either cuz if between the last frame and the current frame you hit a wall, your velocity will become zero and by removing the input_from_last_frame your velocity will become negative which isn't ideal

to sum it all up, what I need is for the velocity that is gained by thing like idk a genrade exploding near the player be calculated sproatley so that at the end I can add the velocity gained form the players input without effecting the velocity gained from the environment and how that is calculated

if that makes any sense coolcry

near vortex
#

Manipulating velocity directly is hard, because you have to account for a lot of different things - like input, gravity, collisions (with the ground, at an angle, at a slope, ...), velocity gain or loss through sliding on slopes - not to mention outside sources like the grenades you mentioned.

That being said, your current use case could just boil down to

  • Create vector from normalized input, current horizontal velocity and player y rotation
  • Clamp that vector's magnitude to a fixed limit (Vector3.ClampMagnitude)
  • Add your current vertical velocity to the vector
  • Optional: interpolate between the calculated vector and the previous frame's velocity vector to create smooth acceleration
  • Set the current velocity to the calculated vector
eager blade
#

thanks:)

#

ok so I was going to implement your solution but I think it had problem (or it doesn't and I don't understand it, in any case I would be very happy if you help me out :)
that is you are limiting the speed entirely but I don't want to do that
what I want is something kinda like in the picture below

near vortex
#

Just add external velocity (like from grenades) during the step I marked as "optional"

eager blade
#

OK thanks

brave garnet
#

A physical way of working it out is to use forces instead of speed. So the input translates to forces. The problem is that then the players could reach very high speeds, and it's hard to stop, which you can fix by adding friction, or limiting the velocity directly. You can also decrease the applied force as the magnitude of the speed increases, which is more or less why you can't run even faster at some point.

#

This way, the collisions will always work well

#

Now, if the granade explodes close to the player, you can just add an impulse force to the player, where the unitary direction is the difference between the two positions (player and granade) divided by the distance, multiplied by a factor that represents the intensity of this impulse.