OnTriggerEnter/Exit2D not working

Hey guys !

I’m nem to unity and have created this basic movement script so i can do some tests and learn a bit following the 2d platformer tutorial but there’s a little problem, here’s the code :

using UnityEngine;
using System.Collections;

public class controller : MonoBehaviour {


    public float speed = 10f;
    Rigidbody2D rb2d;
    public float jumpPower = 1000;
    bool grounded;

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

    void Update ()
    {
        if (grounded && Input.GetKeyDown(KeyCode.Space))
        {
            rb2d.AddForce (new Vector2 (0, jumpPower));
        }
    }

    void FixedUpdate ()
    {
        float move = Input.GetAxis ("Horizontal");

        rb2d.velocity = new Vector2 (speed * move, rb2d.velocity.y);

    }

    void OnTriggerEnter2D(Collider2D other)
    {
        grounded = true;
    }

    void OnTriggerExitr2D(Collider2D other)
    {
        grounded = false;
    }

}

my character doesn’t want to jump even if it’s colliding , so that left me wondering if the OnTriggerEnter/Exit2D isn’t working for my script

Any ideas ?

OnTriggerExitr2D ← spelled incorrectly.

You say the word colliding. Does your ground collider have isTrigger enabled? If not, OnTriggerEnter/Exit will not fire. You want to use OnCollisionEnter2D(Collision2D enter) and OnCollisionExit(2D Collision2D exit).

If I were you I’d also check if the collider your touching is the ground before setting ‘grounded’.

Something like:

void OnCollisionEnter(Collision2D col)
{
    if (other.gameObject.CompareTag("Ground")) //create a 'Ground' tag and apply it in the inspector
        grounded = true;

}

That way, if you touch other colliders, you wont be considered grounded.

^What BenZed said above i believe is correct, i learnt that the hard way too. Use ‘OnCollisionEnter2D’ though. The reason is because, “OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.” -Unity Script Guide.

This will help you learn how Unity works! Unity - Scripting API: MonoBehaviour
-Cheers and GL