- Home /
Jump Code Not Working (2D C#)
So I'm making a 2D platform game in which I need my character to jump, but I am having some issues. (I am scripting with C#). I am very new to Unity, so keep that in mind. I haven't seen anyone else with my problem, and I've been searching for a while. (Or maybe I misinterpreted how this function is supposed to work and this is intended) This is the code for jumping in my PlayerControl script:
if (Input.GetKeyDown (KeyCode.UpArrow)) {
if (!inAir) {
rb.AddForce(Vector2.up * jumpHeight);
}
}
To me it seems pretty simple and self-explanatory. rb is my RigidBody2D, and jumpHeight is a float. inAir is a boolean I made to check if he is already in the air or not, and this check works perfectly by my testing. The problem? Well, the jump is really tiny and fast, it just looks like my character glitches up and then back down, and the weird part is that no matter what I set the jumpHeight to, it is always the same. If I set it to 50000, it is still a tiny little glitch jump. I would consider myself a intermediately advanced programmer, but I am completely new to Unity, so if there is a Unity related mistake that could be the problem, but you are not saying because it just seems so stupid and obvious, please say it anyway, it might be the problem.
EDIT: Everyone is asking me if this is in the Update() function and it is. In fact, it is in the FixedUpdate() function.
Answer by jhepong · Oct 29, 2015 at 11:26 AM
try adding a type of ForceMode:
rb.AddForce(Vector2.up * jumpHeight, ForceMode.Acceleration);
you can play with the type of ForceMode here
Also check the mass of your object. Ideally it should be between 0.1 and 10, anything larger and problems will occur. http://docs.unity3d.com/ScriptReference/Rigidbody-mass.html
Hey, thanks for the answer, but this doesn't really work. In fact, it creates a compiler error, saying that it cannot convert a UnityEngine.Force$$anonymous$$ode to UnityEngine.Force$$anonymous$$ode2D, and that it's an invalid argument for the AddForce function. I saw there was something called Force$$anonymous$$ode2D, but it doesn't have an acceleration property. It has a force and an impulse mode. Changing it to force mode doesn't change it at all, so I would assume that is the default mode. Changing it to impulse mode creates the opposite problem, where he jumps as high as possible no matter what.
Answer by Nikunj-Kareliya · Oct 29, 2015 at 05:44 AM
Hi @RothX,
Make sure your code is inside Update() method and Rigidbody2D component is attached to the player object.
Good practice is to put your physics based code inside FixedUpdate() method.
void FixedUpdate () {
if(Input.GetKeyDown(KeyCode.Space)) {
rb.velocity = Vector3.Up * jumpHeight;
}
}
I hope, this will help.
Cheers.
Yep, my jump is already in the FixedUpdate method, and I have a RigidBody2d component. I just didn't show it in my code, because there's bunch of other stuff inside it too.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Axis Issue 1 Answer
Distribute terrain in zones 3 Answers
How would you make a pulling/sucking/attracting mechanic? 2 Answers
jump button(2d 2018 lts) 1 Answer