isGrounded not working,is grounded stops players from jumping
When I add in isGrounded to this "if (Input.GetButtonDown ("Jump") && isGrounded) ".
It stops the player from jumping. Below is the player controller, I have added in everything when following the video.
The "Is grounded" automatically unticks when I play the game. I think this might be a problem but I am not sure why this unticks?
My code looks like this. My player stops jumping s soon as I add in && isGrounded. I have no idea what the issue might be. Anyone any help please??
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
private Rigidbody2D myRigidbody;
public float jumpSpeed;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
public bool isGrounded;
// Use this for initialization
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
if (Input.GetAxisRaw("Horizontal") > 0f)
{
myRigidbody.velocity = new Vector3(moveSpeed, myRigidbody.velocity.y, 0f);
}
else if (Input.GetAxisRaw("Horizontal") < 0f)
{
myRigidbody.velocity = new Vector3(-moveSpeed, myRigidbody.velocity.y, 0f);
}
else
{
myRigidbody.velocity = new Vector3(0f, myRigidbody.velocity.y, 0f);
}
if (Input.GetButtonDown ("Jump") && isGrounded)
{ myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);
}
}
}
,When I add in isGrounded to this "if (Input.GetButtonDown ("Jump") && isGrounded) ".
The "Is grounded" automatically unticks when I play the game. I think this might be a problem but I am not sure why this unticks?
My code looks like this. My player stops jumping as soon as I add in "&& isGrounded". I have no idea what the issue might be. Anyone any help please??
using UnityEngine; using System.Collections.Generic; using System.Collections;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
private Rigidbody2D myRigidbody;
public float jumpSpeed;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
public bool isGrounded;
// Use this for initialization
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
if (Input.GetAxisRaw("Horizontal") > 0f)
{
myRigidbody.velocity = new Vector3(moveSpeed, myRigidbody.velocity.y, 0f);
}
else if (Input.GetAxisRaw("Horizontal") < 0f)
{
myRigidbody.velocity = new Vector3(-moveSpeed, myRigidbody.velocity.y, 0f);
}
else
{
myRigidbody.velocity = new Vector3(0f, myRigidbody.velocity.y, 0f);
}
if (Input.GetButtonDown ("Jump") && isGrounded)
{ myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);
}
}
}
Answer by DiGiaCom-Tech · Oct 12, 2018 at 10:02 PM
Not an expert on this but ... if this is physics .. shouldn't this code be within a 'FixedUpdate'? However, it looks like the function you are calling to set 'IsGrounded' returns a Collider not a Boolean so you should be checking if the collider returned is null or not and then setting the 'IsGrounded' flag accordingly. That is, assuming if you are in collision with the 'Ground' then true else false would be something like...
isGrounded = (Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround) != null);
The only other thing that comes to mind is the 'What Is Ground' setting of 'Ground' ... perhaps 'Ground' isn't what you think it is.