Having Issue with Player Jumping Consistently
Hey guys so im making a platform jumper. Player jumps from platform to platform, avoiding obstacles, very normal stuff. So I created my platform for the player using normal variables like "jump, jumpheight, groundcheck, groundcheckradius" and so on. Player jumps fine (he floats a little too much in the air for my liking). Picture the platform as just a normal rectangle at this point, I only want the top of the rectangle to be "solid" so i change the offset and size of the Box Collider so the player can only stand and jump at the top and will just pass through the rest of the rectangle if they dont jump high enough.
Everything is working fine so I copy and paste the platform to have something for the player to jump to. This platform also works fine. Now I copy and paste a third platform a little higher up, this one i decide to make a little smaller (on the X axis), say instead of being 5ft long like the first two, its only 3ft. The player uses it relatively well but some spots its either a delay when he jumps, or other spots I can click the jump button as much as I want and it still doesnt jump. The specs should all be the same, I literally copy and pasted it. I adjusted the offset and size of the Box Collider for the smaller platform too just to make sure it was still only covering the proper part of the platform. Any suggestions? Ill paste my code, thanks!
public class Controls : MonoBehaviour {
public Rigidbody2D rb;
public float movespeed;
public bool moveright;
public bool moveleft;
public bool jump;
public float jumpheight;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool onGround;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
rb.velocity = new Vector2(-movespeed, rb.velocity.y);
}
if (Input.GetKey(KeyCode.RightArrow))
{
rb.velocity = new Vector2(movespeed, rb.velocity.y);
}
if (moveright)
{
rb.velocity = new Vector2(movespeed, rb.velocity.y);
}
if (moveleft)
{
rb.velocity = new Vector2(-movespeed, rb.velocity.y);
}
if (Input.GetKey(KeyCode.Space)&&onGround==true)
{
rb.velocity = new Vector2(rb.velocity.x, jumpheight);
}
if (jump)
{
rb.velocity = new Vector2(rb.velocity.x, jumpheight);
jump = false;
}
{
onGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
}
void FixedUpdate()
{
onGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
}
is it possible that the overlap circle is registering the platform above, and thus preventing the character from jumping? Also not sure what the onGround check within update is doing, it's enclosed with { } but it looks to me like the code would never be accessed at first glance.
Hmm what overlap circle are you referring to exactly? Im not sure why I have onGround check on their twice but when I delete one or the other it doesn't seem to make a difference. It has come to the point where I literally copied the code from the tutorial I am using and it still has the same effects. Its crazy, I have five platforms and the first four work, last one doesn't. The first two are lets say 5' long while the next two are the same platforms copy and pasted but only 3' long. They all work now.
Doesnt seem to matter the length of the platform, i didnt have to edit the offset or anything. Now the fifth one is a duplicate of the 3' long one and you cant jump on it at all. Thats kind of a lie, if you hold the jump button down the player will constantly jump up and down. If i hold down the jump button the whole time from the fourth platform to the fifth, he can jump on the 5th one all day. Its only when I stop and walk on the platform that the player can not jump at all. This is so confusing
Never $$anonymous$$d, I may have figured it out. The tutorial says to set the Ground Check Radius to .1. I bumped it up to .2 and the player seems to jump correctly. Thanks for your help! Had you not brought up the onGround check it may not have led me to that!
Your answer
Follow this Question
Related Questions
Can't jump/buggy when standing next to an object. 0 Answers
How to use Animation Event in this case ? 1 Answer
How to make object with velocity bounce off the obstacles? 0 Answers
Trigger issues 1 Answer
2d Tilemap collidor not working 0 Answers