3
\$\begingroup\$

I am making an endless 2d runner with unity. my player object is not moving at all, i move all the other game objects. I have a SpeedManager script which increases the "speed" at every update by a short amount. The movement speed of every gameObject which is streaming towards the player is now multiplied with the SpeedManager "speed".

One controll of my player is a jump. Currently i am using

public Vector2 jumpForce = new Vector2(0, 350);
rigidbody2D.AddForce(jumpForce);

The problem now is, as the game goes on, the game objects pass faster and faster but the player jump remains the same. What i want is a jump that goes as far as before, which means, the player should jump faster into the air, reach the same height and drop faster to the ground, so the jump distance is the same.

I tried some stuff like multiplying both the jumpForce.y and the gravity of my player object with my SpeedManager.speed, but i did not achieve anything yet. :)

Greetings.

\$\endgroup\$
  • 3
    \$\begingroup\$ you can scale the time to go faster instead of increasing the speed of background moving. \$\endgroup\$ – Ali1S232 Apr 29 '14 at 23:49
  • \$\begingroup\$ Probably more standard solution ( new to unity and gamedev :) ) working great, thx. \$\endgroup\$ – Raigeki Apr 30 '14 at 0:56
3
\$\begingroup\$

Easy way to implement an increasing speed game is to adjust the time scale component. instead of increasing the player speed. keep it constant.

scale the time so that it appears its going fast.

Time.timeScale is 1 for normal speed. increase this in fixed amounts (needless to mention till a certain point otherwise it would just go faster and faster.)

\$\endgroup\$
2
\$\begingroup\$

Hello and welcome to "Linear Acceleration and You: How To Use Math To Throw An Object The Right Way".

Assuming an initial horizontal speed of s (in distance per time unit), a jump speed of j (distance per time unit), maximum jump height of h (distance) and gravity of g (distance per time unit squared). Time to reach maximum jump height is t (time units).

Your basic jump will take t = j/g time units to reach the top of the jump, and 2j/g from start to end. Therefore, it should cover a horizontal distance of d = s*(2j/g).

The maximum jump height, using the basic equation for linear acceleration you learned in Physics 101, is h = g/2 * t² = g/2 * (j/g)² = g/2*j²/g² = j²/2g.

And now we have a two-equation system:

d = s*(2j/g)
h = j²/2g

Five terms. h and d are constant, and we want to know j and g based on a variation of s.

Let's use one of these equations to define j in terms of g.

h = j²/2g
2g = j²/h
g = j²/2h

Now we replace g on the first equation with the above, and try to isolate j:

d = s*(2j/(j²/2h))
d = s*((2j/j²)*2h)
d = s*4h/j
j = s*4h/d

And voila. j is directly proportional to s, and g is directly proportional to the square of s.

Going back to code:

CONSTANT_VALUE_1 = 4 * maxJumpHeight / jumpDistance
CONSTANT_VALUE_2 = 0.5 / maxJumpHeight

jumpForce.y = SpeedManager.speed * CONSTANT_VALUE_1
gravity = jumpForce.y * jumpForce.y * CONSTANT_VALUE_2

I'm not accounting for air friction here or other physics engine shenanigans, so you might need to test various constant values to see if you get the right distance and height.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.