Hello, I’m a newbie when it comes to Unity and Javascript. I’m making a game for a school project, a 2D platformer.
However, I am encountering a problem with my ground check, it’s always false! (Meaning that Unity thinks the player is still in the air, correct?).
There must be something wrong, but I can’t see it. Any help will be appreciated, thanks in advance!
This is my code:
var moveUp : KeyCode;
var moveDown : KeyCode;
var moveLeft : KeyCode;
var moveRight : KeyCode;
var isGrounded : boolean;
var groundCheck : Transform;
var groundRadius : float = 0.2;
var ground : LayerMask;
var jumpForce : int = 700;
var speed : float = 10;
function FixedUpdate ()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, ground);
}
function Update ()
{
if (Input.GetKey(moveUp)) && isGrounded)
{
rigidbody2D.velocity.y = jumpForce;
}
else if (Input.GetKey(moveLeft))
{
rigidbody2D.velocity.x = speed * -1;
}
else if (Input.GetKey(moveRight))
{
rigidbody2D.velocity.x = speed;
}
}
You set a layermask in your check which is a little suspect for someone really new (i.e the last variable, ground). Did you mean to do that and did you configure the layermask in Inspector?
– getyour411