Hello. There you can see my script. I try to make a 2D Jump and Run game. But sometimes if i hold the space button (for jumping) my character makes a unwanted “mega jump”. I think because my character is grounded too long and the script adds the force to the 2d rigidbody two or more times. Can someone help me ?
using UnityEngine;
using System.Collections;
public class CharacterController : MonoBehaviour {
private Rigidbody2D rb;
public float JumpForce = 1000;
public bool isGroundet = false;
public bool Jump = false;
public Transform [] groundCheck;
public LayerMask whatIsGround;
// Use this for initialization
void Start () {
rb = gameObject.GetComponent<Rigidbody2D>();
}
void FixedUpdate () {
rb.velocity = new Vector2(Speed, rb.velocity.y);
if (Input.touchCount > 0 || Input.GetKey(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
Jump = true;
}
isGroundet = false;
if (Physics2D.OverlapCircle(groundCheck[0].position, .15f, whatIsGround))
{
isGroundet = true;
}
if (Jump && isGroundet&&rb.velocity.y<JumpForce)
{
rb.AddForce(Vector2.up * JumpForce);
Jump = false;
}
}
}