- Home /
Character jumps unusually high near platform edges and unusually far when repeatedly tapping movement keys
So I'm figuring out Unity2D by piecing apart the documentation as well as the example that the Unity site has available.
Currently jumping is almost always proper except for two cases.
Case 1
The player jumps at the edge of a platform and jumps around 1.5 to 2x higher than normal. This is only recreatable at platform edge and I suspect it has to do with the fact that I've got the game coded to give the player 10 drag on the ground, and 0 drag in the air, I think it may be an inconsistency with Update and FixedUpdate and I'm not using them correctly.
Here's code that I think's relevant to the problem, the entire script I've written will be available at the bottom in a link.
void Start () {
groundCheck = transform.Find ("groundCheck");
}
void Update () {
if (grounded && Input.GetButtonDown ("Jump")) {
rigidbody2D.AddForce(new Vector2(0, 1000f));
}
}
void FixedUpdate () {
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
if(grounded)
rigidbody2D.drag = 10;
else
rigidbody2D.drag = 0;
...
}
Case 2
This is the case where the character can go unusually far when jumping. It can happen regardless of where the player jumps, and cannot be recreated by tapping repeatedly while on the ground. It seems like the character gets a one frame acceleration before getting floored by the speed limit, but pressing fast enough it allows for a 1.5 increase in jump distance.
Again, part of the code that I suspect is relevant, whole thing available at the bottom.
void FixedUpdate () {
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
if(grounded)
rigidbody2D.drag = 10;
else
rigidbody2D.drag = 0;
float h = Input.GetAxis("Horizontal");
if (Mathf.Abs(h * rigidbody2D.velocity.x) < 5f)
rigidbody2D.AddForce (Vector2.right * 365f * h);
if (Mathf.Abs (h * rigidbody2D.velocity.x) > 5f || Mathf.Abs (rigidbody2D.velocity.x) > 5f)
rigidbody2D.velocity = new Vector2 (Mathf.Sign (rigidbody2D.velocity.x) * 5f, rigidbody2D.velocity.y);
...
}
Hopefully this is formatted well enough to be legible, here is the remainder of the code in the script.
Answer by Sir Lulzalot · Jun 07, 2014 at 02:53 AM
Okay, this is awkward because I fixed it myself, but for those curious.
Case 1
Okay definitely, keep your variables in the same Update loop as ANY LOGIC CASE THAT NEEDS IT. I just had to push them all into Update, and everything was fine.
Edit: Apparently it's better to put them in FixedUpdate since they're physics related.
Edit2: It causes other bugs to put a buttonpressdown in FixedUpdate, so I'm leaving it in Update since it has yet to fail there.
Case 2
I just had to add to my acceleration if statement an addendum of rigidbody2D.velocity.x < 5f to prevent going over the top (5f being the max).
if (grounded && Input.GetButtonDown ("Jump")) {
rigidbody2D.AddForce(new Vector2(0, 1000f));
I was going to mention it (but I got called away before you fixed it yourself) this rigidbody2D.AddForce is a physics call and therefore should actually be in the FixedUpdate section. because FixedUpdate handles physics.
So I would suggest trying to add everything to the FixedUpdate rather than the Update.
Ah okay, thanks! I just figured buttonpress-based things should be in Update. Would you happen to know a link to (or what in general) what should be placed in Update()?
Edit: I mention it in my answer now, but FixedUpdate causes another bug where it doesn't register buttonpressdown 2/3rds of the time.