- Home /
Character instantly jumps instead of smoothly gaining altitude
Hi everyone, I'm working on a 2D sidescroller using the new 2D features from 4.3. To start off, I created a player movement script that uses the 4.3 workflow demo as a base. I've copied their jump scripting to a tee, but for some reason, my character's jump works unusually. Instead of rising gradually, he shoots up, and then descends normally (as seen below). The rigidbody2D settings are exactly the same as well.
Here is the script I'm using to jump:
bool jump;
void Update(){
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
if (Input.GetButtonDown("Jump") && grounded){
jump = true;
}
}
void FixedUpdate(){
if (jump){
rigidbody2D.AddForce(new Vector2(0f, 1000));
jump = false;
}
}
I am having the same issue even with the 'Apply Root $$anonymous$$otion' already turned off...
my 2d game has been doing the same thing and i fixed it by adding not script to the character but animation
Answer by Captain_Dando · Nov 20, 2013 at 07:38 AM
Ok, I initially closed this question because I thought the answer had nothing to do with the script, which is true, but I've discovered the source of the problem and I'd like to share it for anyone else who's battling with this problem. when I turned the "Apply Root Motion" option off on the animator component my character was able to move smoothly and the problem disappeared. I'm not entirely sure why this was a problem but I think it may have something to do with the fact that the demo player character was made of multiple parts which moved separately as opposed to the single sprite animation which I used. in any case, turn it off and the problem should be resolved
Answer by gajdot · Nov 19, 2013 at 08:01 AM
The problem is that you add a huge force to it, which blows him up in the air seemingly teleporting up instantly.
I believe you could fix this by applying a smaller value over time, so it would transition a bit more nicely.
So basically add a smaller force to it and don't set the jump to false instantly, instead make a timer and set it to false only a second later.
You can do this two ways co-routine or just add a Time variable and when jump is true add deltaTime to it. When this value gets higher then the threshold and jump is true then set jump to false and zero down the time variable.
I've thought of doing this, but I'm concerned that it will create bugs if the jump coroutine gets interrupted. also, I'm not sure why, but in the 2d example project the jump works perfectly, even at 1000 as well, yet with $$anonymous$$e as you can see it's instant. setting the velocity of the character along the y axis to any value does the same thing as well
To be honest I didn't checked the example, so I don't know how does it work there, but I'll see into that. $$anonymous$$eanwhile you could try the way I suggested without coroutine with the Time variable update in the fixedUpdate.
Check what's your mass of your character, in the example project it's only 1. If you have higher mass that can make the difference. If not, I don't know what's the difference, but the timer can solve your problem.
Ok, I've replicated my initial code in a completely new project and strangely enough the problem is resolved. I'm not sure what caused it, but it's nothing in the script, so I'm going to close the question
Answer by Lazzle · Nov 14, 2018 at 10:23 PM
Just came across this problem myself, so just incase anyone else comes across the same problem: in my case the problem was that i was resetting the Y velocity in my movement controls
RB.velocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical") ;
so the upward velocity is getting zero'd in the next frame and the character starts falling (the characters downward velocity is also zero'd each frame, hence why he falls at a constant rate, rather than accelerating like normal)
Fix: RB.velocity = new Vector3(Input.GetAxis("Horizontal") , RB.velocity.y, Input.GetAxis("Vertical"));
My assumption is that in the case above some other script that also acts on the character is doing something similar (resetting the velocity every frame)
Your answer
Follow this Question
Related Questions
How to calculate the rb.AddForce for my game? 1 Answer
Rigidbody2D and AddForceAtPosition 1 Answer
Rigidbody2D AddForce only working with large numbers 2 Answers
Jump problem 1 Answer
Unity 2D Physics .AddForce 1 Answer