Hi,
I’m writing a code for my platformer game .I have an issue with double jump .When I jump after 3 jump I’m not able to jump anymore also when I collide with object in scene.
My code is:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float jumpHeight;
private Rigidbody2D rigi;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
// Use this for initialization
private void Awake()
{
rigi = GetComponent<Rigidbody2D>();
}
void Start () {
}
void FixedUpdate () {
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius,whatIsGround);
}
// Update is called once per frame
void Update () {
if (grounded)
doubleJumped = false;
if(Input.GetKeyDown (KeyCode.Space) && grounded)
{
//rigi.velocity = new Vector2(rigi.velocity.x, jumpHeight);
Jump ();
}
if(Input.GetKeyDown (KeyCode.Space) && !doubleJumped && !grounded)
{
//rigi.velocity = new Vector2(rigi.velocity.x, jumpHeight);
Jump();
doubleJumped=true;
}
if(Input.GetKey (KeyCode.RightArrow))
{
rigi.velocity = new Vector2(moveSpeed, rigi.velocity.y);
}
if(Input.GetKey (KeyCode.LeftArrow))
{
rigi.velocity = new Vector2(-moveSpeed, rigi.velocity.y);
}
}
public void Jump(){
rigi.velocity = new Vector2(rigi.velocity.x, jumpHeight);
}
}
Thanks