My problem is this, in this game, when I jump, I programmed that the character could not move in the air, what is the problem? that, in addition to the floor, there is another collider, the box, and by brushing the box with your foot in mid-air, you can move while you are floating, the logical thing would be to do an "OnCollisionExit2D" But when I do that, I can no longer push the box, because of course, when I stop touching the box I can no longer move and the character stays static, so if you move the box, you can't get off, my idea would be to create a timer, so to speak " If you are not touching the box for 1 second, air = true" (Air is the variable that you create so that the character does not move in the air) Here is the code (Solid represents the box and Floor the platform)
bool canJump = true;
bool DontMove = true;
public bool air;
if (air == true)
{
gameObject.GetComponent<Animator>().SetBool("Moverse", false);
gameObject.GetComponent<Rigidbody2D>().drag = 0;
DontMove = false;
canJump = false;
if (canJump == false)
{
gameObject.GetComponent<Animator>().SetBool("Saltar", true);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.transform.tag == "Piso")
{
gameObject.GetComponent<Rigidbody2D>().drag = 1;
DontMove = true;
canJump = true;
gameObject.GetComponent<Animator>().SetBool("Saltar", false);
air = false;
}
if (collision.transform.tag == "Solido")
{
gameObject.GetComponent<Rigidbody2D>().drag = 1;
DontMove = true;
canJump = true;
gameObject.GetComponent<Animator>().SetBool("Saltar", false);
air = false;
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.transform.tag == "Piso")
{
air = true;
}
if (collision.transform.tag == "Solido")
{
air = true;
}
}
if (Input.GetKey("a") && DontMove && Stop == false)
{
if (canJump == true)
{
gameObject.GetComponent<Animator>().SetBool("Moverse", true);
}
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(-1200f * Time.deltaTime, 0));
gameObject.GetComponent<SpriteRenderer>().flipX = true;
}
if (Input.GetKey("d") && DontMove && Stop == false)
{
if (canJump == true)
{
gameObject.GetComponent<Animator>().SetBool("Moverse", true);
}
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(1200f * Time.deltaTime, 0));
gameObject.GetComponent<SpriteRenderer>().flipX = false;
}
if (!Input.GetKey("d") && !Input.GetKey("a"))
{
gameObject.GetComponent<Animator>().SetBool("Moverse", false);
}
if (Input.GetKeyDown("w") && canJump && Stop == false)
{
air = true;
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 1000f));
}
In the end I did this:
I think it may be more effective if you use a raycast2d to detect the floor. I wouldn't recommend using a rigidbody, because, as it's proprietary physics controlled by Unity, it doesn't give me security. You could make a small gravity system for the jump, I feel that it is more effective, but they are preferences. Regarding dependencies it is better to create a dependency for Animator, because it takes up less memory. I don't know what kind of jump you want to do, but I'll leave you with a video that helps a lot about jumps for 2D characters and maybe it's what you're looking for: https://www.youtube.com/watch?v=QGDeafTx5ug&t= 513s