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);
}
}
}