I’m trying to make a 3D platformer. Can someone please tell me what’s wrong with my code?
Okay, given your description the problem is the following:
When you press space, you change the y-velocity of your object to something unequal 0.
Then in Update
you save this value in playerDirection
. In FixedUpdate
you take the current y-Velocity, multiply it with jumpforce
which I assume is larger than 1 and set this value as new velocity. Given this you basically add way more velocity each frame than gravity could ever hope to counter.
What you might want to do is to remove the multiplication by jumpforce
in FixedUpdate. This will probably already fix your issue.
Next time: Add more details. Way more details. You expect others to help you, to spend time for you and solve your problems for free. Put in the effort to write at least 5 sentences laying out what is going wrong. This is good for everyone as it also helps you get your problem solved faster.
Good luck, let me know if the solution worked for you.
My problem is that when I jump, I fly into space.
Did you make sure that you have a “Rigid Body” component to your object and then make sure it has “Use Gravity” checked!
Patrick