Предел прыжка для игрока - PullRequest
0 голосов
/ 03 мая 2019

Мой игрок прыгает непрерывно, и я хочу, чтобы он прыгал всего 2 раза, не касаясь земли (если он коснется земли, он умрет). Как я могу ограничить это?Должен ли я сделать еще один сценарий?Вот скрипт игрока.Я также хочу заставить его не вращаться, когда он падает с коллайдера на другой боксерский коллайдер.Моя игра 2D игра.

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 Ответ

1 голос
/ 03 мая 2019

Просто добавьте счетчик, например,

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

и сбросьте его, когда игроку будет разрешено прыгнуть снова

jumpCount = 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();
    }

    jumpCount = 0;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...