0

Как ограничить количество прыжков до 2? Просто у меня получается много прыжков.

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

public class hero : MonoBehaviour
{
    Rigidbody2D rb;
    Animator anim;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            jump();
        }

        if (Input.GetAxis ("Horizontal") == 0)
        {
            anim.SetInteger("Temp", 1);
        } 
        else
        {
            Flip();
            anim.SetInteger("Temp", 2);
        }
    }

    void Flip()
    {
        if (Input.GetAxis("Horizontal") < 0)
            transform.localRotation = Quaternion.Euler(0, 0, 0);

        if (Input.GetAxis("Horizontal") > 0)
            transform.localRotation = Quaternion.Euler(0, 180, 0);
    }

    void FixedUpdate()
    {
        Vector2 movement = new Vector2(Input.GetAxis("Horizontal") * 5f, rb.velocity.y);
        rb.velocity = movement;
    }

    void jump()
    {
        
        rb.AddForce(transform.up * 5f, ForceMode2D.Impulse);
        
    }
}
1
  • 1
    Заведите счетчик.
    – And
    5 апр 2020 в 16:37

1 ответ 1

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

public class hero : MonoBehaviour
{
    Rigidbody2D rb;
    Animator anim;
    Int jump_study=0;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {

            if (jump_study<2){
               jump();
             }
            if (jump_study>0){
              jump_study--;
            }
        }

        if (Input.GetAxis ("Horizontal") == 0)
        {
            anim.SetInteger("Temp", 1);
        } 
        else
        {
            Flip();
            anim.SetInteger("Temp", 2);
        }
    }

    void Flip()
    {
        if (Input.GetAxis("Horizontal") < 0)
            transform.localRotation = Quaternion.Euler(0, 0, 0);

        if (Input.GetAxis("Horizontal") > 0)
            transform.localRotation = Quaternion.Euler(0, 180, 0);
    }

    void FixedUpdate()
    {
        Vector2 movement = new Vector2(Input.GetAxis("Horizontal") * 5f, rb.velocity.y);
        rb.velocity = movement;
    }

    void jump()
    {

        rb.AddForce(transform.up * 5f, ForceMode2D.Impulse);
        jump_study++;

    }
}
1
  • All comiler errors have to be fixed before you can enter playmode!
    – Young Frog
    6 апр 2020 в 10:52

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.