I've got a problem iam stuck with since 2 days and I can't seem to figure it out.
I have a 2d endless runner in unity with a WorldGenerator and a WorldMover Script. The WorldGenerator uses templates to create the world and the MapMover moves each template to the left so it looks like my character is running all the time.
My CharacterController only handles jumps. So faar my game works perfectly.
But here comes my problem the worldSpeed Chages it gets faster and also can get slower if that happens my characters jump distance (the amount of map scrolling underneath him untill he lands again) changes which makes sense but it makes creating a world impossible since the speed doesn't change at fixed points in the game there are slow tiles and power ups which changes the worldSpeed.
I tryed endless different approaches to have the player always jump the same height, (distance) and projection. But I cannot seem to figure out a working formula to archive this. Also I don't want my character to move on the x axis at all.
In my head it should be as easy as take the original jump let's say when the world moves at speed 5 and just speed the jump up so at speed 5 it takes my character like 1 second to complete the jump so on speed 10 it should take 0.5 seconds. But I cannot get that to work. I tryed changing the gravity and jump force in different formulars with supplying the current world speed but I failed here too most of the time the jump is not the same as before the height, distance or projection always changes. Which is the opposite of what I want.
Hopefully someone can point me in the right direction I be thankful for every Tipp or peace of information I can get.
The problem boils down to having to determine the initial velocity and gravitational acceleration of a projectile (disregarding air friction) with a fixed maximum height and a fixed time to land.
You can look up the fromulas for maximum height and time to land:
(maximum height)
(time to land)
These can be rearranged for the gravitational acceleration and merged into a formula that can be used to calculate the velocity.
With the calculated velocity you can use a previous formula to determine the gravity.
float v,g,t,s;
v = 4 * s / t;
g = -2 * v / t;
As long as you pick positive values (greater than 0) for t
and s
you will be able to apply the calculated g
and v
in your own game system exactly like you intended.