- Home /
Running and Jumping problems of a 2D Endless Runner
Hey guys, its been few days and I am struggling with a very simple sounding problem.
I am working on a quick 2D endless runner which which basically just runs and jumps on a platform above then jump down on a platform below. Now the simple part which makes the programming complicated is that the platforms are always generated randomly but at same distance of X and Y.
Now I have tried to work with Rigidbody.Addforce. It can work fine to give the player horizontal speed but I simply can't get it work out the jumping. The deal here is the player must land on the platform above or below to collect the item but using the Rigidbody.Addforce the player just jumps and flys off and/or the velocity just Fs it up. (PS I don't want to turn the horizontal force off as I want the cube to keep moving forward)
Is there a way around this problem ? I have searched the forums and answers a lot but cannot quite find solution. Thanks for the help guys.
Answer by IvovdMarel · Jul 08, 2014 at 03:40 PM
If you want to make the player jump, think about what happens what actually happens when you jump. A force gets added at the moment you push yourself away from the ground, but no extra force gets added while you're in the air. Instead, because of gravity, you'll come down again. Make sure you use both AddForce (only once) and set the gravity in the rigidbody inspector
That's all you need for a jumping script. Also check out the Unity2D tutorial with the bean-guy.
So I should turn the horizontal addforce off when I add the jump force ? Also should I use Force$$anonymous$$ode.Impulse ?
Answer by Enigmaticall · Jul 08, 2014 at 05:03 PM
It's tough to answer without your code to reference, but is it possible that you are using Input.GetKey instead of Input.GetKeyDown for your jumping key? You only want your character to add the force once, so make sure you are using Input.GetKeyDown