Hello, I'm fairly new to scripting and I'm having an issue with grounding my 2D player. I'm drawing a raycast down to tell if I'm grounded, The ray starts 3.13 from the ground. When I put distance to anything less then 3.13 is says I'm not grounded, but anything equal or higher then 3.13, works, but I can double jump and after the second jump, then grounded is turned off. I thought I wrote the code to say that a ray less then 3.13 in distance will be considered grounded, but it is not working for some reason. Any help with this will be greatly appreciated or a simpler code would also be great. I feel like this should be easier then what I'm doing. I got this from a tutorial that was older. Thanks!
public bool isGrounded;
public float distance = 3.13f;
void Update ()
{
PlayerMoving ();
PlayerRaycast ();
}
void PlayerMoving()
{
moveX = Input.GetAxis ("Horizontal");
if (isGrounded == true && Input.GetButtonDown ("Jump"))
{
Jump ();
}
void Jump()
{
GetComponent ().AddForce (Vector2.up * playerJumpPower);
isGrounded = false;
}
void PlayerRaycast()
{
RaycastHit2D RayDown = Physics2D.Raycast (transform.position, Vector2.down);
if (RayDown != null && RayDown.collider != null && RayDown.distance < distance)
{
isGrounded = true;
}
}
↧