How to calculate velocity so a 2.5D platformer character can shoot straight while running on a moving platform?

Hello everyone. I’m working on a platformer with a running cat that has a gun on top, a gun that shots projectiles. The gun turns in such a way that it aims at the cursor. Since the cat was running continuously, when I tried to shot, the projectiles were apparently going backwards relative to the cat. I found a solution that involved getting the velocity of the character using the node " Get Velocity" , then adding it to the velocity of the projectile after it spawned. That worked well, however when the character got on a moving platform, the platform’s velocity was not added to the projectile.

As explained here: How do I account for player motion when firing a projectile? - #4 by Evilraptor

The character’s velocity is internal so moving platforms don’t affect it. In that case, I decided to calculate it externally and built this graph :

While running in a straight line, the printed velocity value seems the same as the “Get Velocity” solution, and it makes the projectile go straight as expected, but only if the character is not going up and down. I guess that I need to calculate velocity in such a way that it takes into account multiple axis/vectors so it gives similar results as the “Get Velocity” node.

I also tested replacing the forward vector with up vector and print the values, and fortunately they do change while on the up/down platform. Still, the projectile trajectory was not compensated, maybe because the lack of the X component.

So…,how to calculate velocity in such away that projectile’s trajectory is compensated regardless if the character is on moving platforms or not ?

Hi there,

Just to give you an insight, not a proper solution, but I would try this way: your final velocity could be the sum of two factors calculated separately:

  1. Horizontal: The max of get player velocity (only X axis) and max walking speed (character movement component) converted to vector, so you always have a horizontal factor
  2. Horizontal: The max of get player velocity (Z Axis) and platform velocity (Z Axis)

To break the velocity, you can call the nodes “break vector” and “make vector”. Max Walking Speed is a float variable taken from “get velocity”+ “vector length” and you need to convert back to vector.

Hope that makes sense.

1 Like

Unfotunately, it’s still not very clear for me what I could do. I tried adding the X component of Get Player velocity to Max walking speed, but it only adds a small offset. I’m not sure how I could convert Max Walking speed back to vector.

As for the Vertical axis, I added the Z of Get player velocity node to the Z velocity of the platform as you suggested, but still the projectiles are not going straight while on the moving platform.

Maybe the problem lies in the way I’m calculating the platform’s velocity, I’m not sure if I’m doing it the right way:

I also recorded a short video with the problem. The print screen values are for platform’s Z velocity.