I don't understand why doesn't it work...

Hello everyone, I’m new here, and i just began a 2D platform game. In the end, the player has to defeat the lion, which goes towards the player. When the player goes from one side of the lion to its other, the lion is supposed to look on the other side, and I don’t know why, it doesn’t work. In this case, I used an animator bool, and a box collider above the lion. When the player goes trough it one time, the bool is supposed to change to “true”, and when the player does it again, it is supposed to get false again. All this linked to the two animations of the lion, one facing right, the other one facing left.

And here’s the code of the “wall” which stands above the lion (I’m french so please excuse me if I made some spelling mistakes; Here “vraifaux” stands for “truefalse”):

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

public class WallToFlip : MonoBehaviour {
    public Animator lionanimation;
    public bool vraifaux;
    // Use this for initialization
    void Start () {
        lionanimation = GameObject.FindGameObjectWithTag ("lion").GetComponent<Animator> ();
        vraifaux = false;
    }
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Player")
        {
            vraifaux = !vraifaux;
        }
    }
    void Update(){
        lionanimation.SetBool ("facingRight", vraifaux);
    }
}

And here is the code of the lion:

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

public class LionAI : MonoBehaviour {

    public float speed = 0.5f;
    public Transform player;

    // Use this for initialization
    void Start () {
        player = GameObject.FindGameObjectWithTag ("Player").transform;
    }

    // Update is called once per frame
    void Update () {
        Vector3 displacement = player.position -transform.position;
        displacement = displacement.normalized;
        if (Vector2.Distance (player.position, transform.position) > 1.0f) {
            transform.position += (displacement * speed * Time.deltaTime);
        }

    }
}

Thanks for your understanding, and I wish to all of you a pleasant day.

v1ruso

Can you add a print function to your onTriggerEvent to confirm that it is being triggered by the Player moving through it?

Thanks for your answer.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallToFlip : MonoBehaviour {
    public Animator lionanimation;
    public bool vraifaux;
    // Use this for initialization
    void Start () {
        lionanimation = GameObject.FindGameObjectWithTag ("lion").GetComponent<Animator> ();
        vraifaux = false;
    }
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Player")
        {
        Debug.Log ("Detected");
            vraifaux = !vraifaux;
        }
    }
    void Update(){
        lionanimation.SetBool ("facingRight", vraifaux);
    }
}

As you can see, I’ve added Debug.Log, and it seems that the ontriggerevent hasn’t been triggered by the player…
Thank you for your help

Okay I understood where was my mistake, i put a box collider above the lion, and not a box collider 2d…

Glad it helped! I will be interested to see if others have a ‘better’ way of seeing if trigger events are occurring? I have always just used a simple debug.log to achieve the same thing.

Glad you solved your problem (PS keep up the good work, your code is very easy to follow).

Thank you very much :wink: