help with OnTriggerEnter (Collider xxx)

Hi folks,

pretty new to scripting but i manage to understand a little with the help of the good guys here :slight_smile:

I am trying to make a flappy bird kind of game. I have got my script on my player moving and flying. Now i want to add a function in my script that when my player collides with the ground or any objects, it dies and it changes visually (change in the animation).

I have put my script down here, can anyone please advise how i should use the OnTrigger function. I’ve read the documentation, tried a lot of stuff but it doesn’t work.

thanks a lot for your help

Seb

using UnityEngine;
using System.Collections;

public class MouvementJoueur : MonoBehaviour {

    Vector3 velocite = Vector3.zero;
    public Vector3 gravite;
    public Vector3 pousseeVelocite;
    public float vitesseMax=5f;
    public float vitesseavant =1f;
    bool poussee = false;

    void Update(){
        if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
            poussee = true;
        }
    }

    void FixedUpdate () {
        velocite += gravite * Time.deltaTime;
        velocite.x = vitesseavant;
        if(poussee == true) {
            poussee = false;
            if(velocite.y < 0)
                velocite.y = 0;
            velocite += pousseeVelocite;
        }

        velocite = Vector3.ClampMagnitude (velocite, vitesseMax);
        transform.position += velocite * Time.deltaTime;

        float angle = 0;
        if(velocite.y < 0) {
            angle = Mathf.Lerp(0,-90, -velocite.y / vitesseMax);
        }

        transform.rotation = Quaternion.Euler(0,0,angle);
    }

    void OnTriggerEnter (Collider "obstacle") {
        //?????????????????  what do i enter here so my player collides with the ground and change his costume (or animation)?  thanks!!
    }

}

What kind of animation do you want to set up? Animator or something else? It depends on how you made your animation.

One thing that you did wrong. The argument should be without hooks. It is not a string, but a variable.

    void OnTriggerEnter (Collider obstacle) { 
    // You probably want to make a few types of colliders: death (leads player to death) and score (adds some scores).
     if(obstacle.tag == "Score")
     {
     AddScore();
     }else if (obstacle.tag == "Death")
     {
     // Tell me what kind of animation you have
     }

}

Thanks… What i would like is that my variable vitesseavant drops to 0 and i’ve set an animation in my animator and i want to call that animation. Should i go with OnCollisionEnter or OnTriggerEnter?

It depends on what you want it look like. I think trigger is fine. In case of collision, you will be just stopped before the wall immediately. Flappy Bird, I would say, used “Trigger” kind of mechanism for that.

Btw, did you turned on “IsTrigger” checkbox in collider?

Have you already set up everything in animator? Did you set up animator parameters and connected them to proper animations?

Thanks for your help.

Actually, my player has a RigidBody attached to it and a collider without the trigger. My ground has a collider that is a trigger. Yes, i have setup my animation, and i have a trigger parameter on it that is named Dead.

Which coding should i use? Do i have to put a “tag” on my ground and setup my scripting so it detects that tag, and then go in the animator to play the good animation?

Seb

Well, yeah. That will work.

Actually, i tried it with this script… But it doesn’t work.

using UnityEngine;
using System.Collections;

public class MouvementJoueur : MonoBehaviour {

    Vector3 velocite = Vector3.zero;
    public Vector3 gravite;
    public Vector3 pousseeVelocite;
    public float vitesseMax=5f;
    public float vitesseavant =1f;
    bool poussee = false;


    Animator animator;

    bool dead = false;

    void Update(){
        if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
            poussee = true;
        }
    }

    void FixedUpdate () {

        if (dead)
            return;
        velocite += gravite * Time.deltaTime;
        velocite.x = vitesseavant;
        if(poussee == true) {
            poussee = false;
            if(velocite.y < 0)
                velocite.y = 0;
            velocite += pousseeVelocite;
        }

        velocite = Vector3.ClampMagnitude (velocite, vitesseMax);
        transform.position += velocite * Time.deltaTime;

        float angle = 0;
        if(velocite.y < 0) {
            angle = Mathf.Lerp(0,-90, -velocite.y / vitesseMax);
        }

        transform.rotation = Quaternion.Euler(0,0,angle);
    }

    void OnTriggerEnter (Collider "obstacle") {
        animator.SetTrigger ("Dead");
        dead=true;
        
    }
}

Unfortunately it doesn’t work. Says Unexpected symbol “obstacle”. Do i have to define it another way? Thanks

I tried detailing it a little more, still not working :frowning:

using UnityEngine;
using System.Collections;

public class MouvementJoueur : MonoBehaviour
{

    Vector3 velocite = Vector3.zero;
    public Vector3 gravite;
    public Vector3 pousseeVelocite;
    public float vitesseMax = 5f;
    public float vitesseavant = 1f;
    bool poussee = false;

    Animator anim;

    bool dead = false;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
        {
            poussee = true;
        }
    }

    void FixedUpdate()
    {
        if (dead)
            return;
        velocite += gravite * Time.deltaTime;
        velocite.x = vitesseavant;
        if (poussee == true)
        {
            poussee = false;
            if (velocite.y < 0)
                velocite.y = 0;
            velocite += pousseeVelocite;
        }

        velocite = Vector3.ClampMagnitude(velocite, vitesseMax);
        transform.position += velocite * Time.deltaTime;

        float angle = 0;
        if (velocite.y < 0)
        {
            angle = Mathf.Lerp(0, -90, -velocite.y / vitesseMax);
        }

        transform.rotation = Quaternion.Euler(0, 0, angle);
    }

    void OnTriggerEnter(Collider collider)
    {
        if (collider.tag== "obstacle")
        {
            return;
        }
        anim.SetTrigger("Dead");
       dead = true;

    }

}

The tag of the ground is “obstacle”, isn’t it? And collision with the ground should kill the player. However, your code does opposite.

    void OnTriggerEnter(Collider collider)
    {
        if (collider.tag== "obstacle") // If collider's tag is obstacle
        {
            return; // Exit the function (stop it here)
        }

       // If code continued, then it means that tag is NOT "obstacle"

      // Do dead stuff, bla, bla, bla
        anim.SetTrigger("Dead");
       dead = true;

    }

Put the code that is after the IF statement, inside it (and remove “return”).

One very important thing. You wrote a few messages ago:

The script you gave, it seems to be the player’s script. But the method OnTriggerEnter will work only if collider on this object is trigger. So, if the trigger is obstacle, and this script is on a player, then it will not work. You should either choose player to be a trigger, or write another script for the obstacles that will recall some dead function in the player.

Cool, now it works!!!

thanks a lot!