M
M
Mpa3buHa2019-11-03 18:55:39
Unity
Mpa3buHa, 2019-11-03 18:55:39

Infinite jump I change the script, in theory the infinite jump should disappear, but the jump disappears altogether?

This is what the code looks like when the infinite jump works:

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

public class Jump : MonoBehaviour
{

    Rigidbody2D rb;
    public bool ReadyJump;
    public bool ReadyGround;
    public int ForceJump;


    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }


    void FixedUpdate()
    {
        if(ReadyJump == true)
        {
            rb.AddForce(new Vector2(0, ForceJump));
            ReadyJump = false;
           
        }
    }
        
    public void JumpPlayer()
    {
        ReadyJump = true;
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Ground")
        {
            ReadyGround = true;
        }
        else
        {
            ReadyGround = false;
        }
    }
}

Like this, when you try to fix the infinite jump and the jump stops working:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Jump : MonoBehaviour
{

    Rigidbody2D rb;
    public bool ReadyJump;
    public bool ReadyGround;
    public int ForceJump;


    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }


    void FixedUpdate()
    {
        if(ReadyJump && ReadyGround)   // Изменено тут
        {
            rb.AddForce(new Vector2(0, ForceJump));
            ReadyJump = false;
            ReadyGround = false;  // Изменено тут
        }
    }
        
    public void JumpPlayer()
    {
        ReadyJump = true;
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Ground")
        {
            ReadyGround = true;
        }
        else
        {
            ReadyGround = false;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question