Hello!
My problem is it when player jump the OnColissionExit doens’t not active allowed the player jumping twice.
What I’m doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour {
public float forcaPulo = 200;
public float velocidadeMaxima = 2;
public int vidas;
public int rings;
public Text TextLives;
public Text TextRings;
private bool canJump;
void Start ()
{
TextLives.text = vidas.ToString();
TextRings.text = rings.ToString();
canJump = true;
}
void Update ()
{
float movimento = Input.GetAxis("Horizontal");
Rigidbody2D rigidbody = GetComponent<Rigidbody2D>();
rigidbody.velocity = new Vector2(movimento * velocidadeMaxima, rigidbody.velocity.y);
if(movimento < 0)
{
GetComponent<SpriteRenderer>().flipX = true;
}
else if(movimento > 0)
{
GetComponent<SpriteRenderer>().flipX = false;
}
if(movimento > 0 || movimento < 0)
{
GetComponent<Animator>().SetBool("Walking", true);
}
else
{
GetComponent<Animator>().SetBool("Walking", false);
}
if(Input.GetKeyDown(KeyCode.Space) && canJump)
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, forcaPulo));
}
}
void OnCollisionEnter(Collision other)
{
if(other.collider.tag == "Player")
{
canJump = true;
GetComponent<Animator>().SetBool("jumping", false);
}
}
void OnCollisionExit(Collision other)
{
if (other.collider.tag == "Player")
{
canJump = false;
GetComponent<Animator>().SetBool("Jumping", true);
}
}
}