Limit of jump for the player

Asked by At

My player jumps continuously and I want to make it jump just 2 times without touching the ground ( if he touches the ground he will die) How can I restrict this ? Should I make another script? Here is the player script . I also want to make him not rotate when he fall from a box collider to another box collider . My game is a 2D game.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    public float moveSpeed = 300;
    public GameObject character;
    private Rigidbody2D characterBody;
    private float ScreenWidth;
    private Rigidbody2D rb2d;
    private Score gm;
    public bool isDead = false;
    public Vector2 jumpHeight;

    void Start()
    {
        ScreenWidth = Screen.width;
        characterBody = character.GetComponent<Rigidbody2D>();
        gm = GameObject.FindGameObjectWithTag("gameMaster").GetComponent<Score>();
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (isDead) { return; }
        if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))  //makes player jump
        {
            GetComponent<Rigidbody2D>().AddForce(jumpHeight, ForceMode2D.Impulse);
        }

        int i = 0;
        while (i < Input.touchCount)
        {
            if (Input.GetTouch(i).position.x > ScreenWidth / 2)
            {
                RunCharacter(1.0f);
            }

            if (Input.GetTouch(i).position.x < ScreenWidth / 2)
            {
                RunCharacter(-1.0f);
            }

            ++i;
        }
    }    

    void FixedUpdate()
    {
#if UNITY_EDITOR
        RunCharacter(Input.GetAxis("Horizontal"));
#endif 
    }

    private void RunCharacter(float horizontalInput)
    {
        characterBody.AddForce(new Vector2(horizontalInput * moveSpeed * Time.deltaTime, 0));
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("ground")) // this will return true if the collision gameobject has ground tag on it.
        {
            isDead = true;
            rb2d.velocity = Vector2.zero;
            GameController.Instance.Die();
        }
    }

    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.CompareTag("coin"))
        {
            Destroy(col.gameObject);
            gm.score += 1;
        }
    }
}

1 Answers

1
derHugo On

Just add a counter maybe like

private int jumpCount = 0;

...

    if (jumpCount < 2 && (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space)))
    {
        // You already have that reference from Start
        // should avoid to use GetComponent again
        rb2d.AddForce(jumpHeight, ForceMode2D.Impulse);
        jumpCount++;
    }

and reset it when the player shall be allowed to jump again

jumpCount = 0;

probably e.g. in

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("ground")) // this will return true if the collision gameobject has ground tag on it.
    {
        isDead = true;
        rb2d.velocity = Vector2.zero;
        GameController.Instance.Die();
    }

    jumpCount = 0;
}