Search Unity

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

Discussion in '2D' started by v1ruso, Aug 21, 2017.

  1. v1ruso

    v1ruso

    Joined:
    Aug 21, 2017
    Posts:
    4
    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"):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WallToFlip : MonoBehaviour {
    6.     public Animator lionanimation;
    7.     public bool vraifaux;
    8.     // Use this for initialization
    9.     void Start () {
    10.         lionanimation = GameObject.FindGameObjectWithTag ("lion").GetComponent<Animator> ();
    11.         vraifaux = false;
    12.     }
    13.     void OnTriggerEnter2D(Collider2D other)
    14.     {
    15.         if (other.tag == "Player")
    16.         {
    17.             vraifaux = !vraifaux;
    18.         }
    19.     }
    20.     void Update(){
    21.         lionanimation.SetBool ("facingRight", vraifaux);
    22.     }
    23. }
    24.  
    And here is the code of the lion:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LionAI : MonoBehaviour {
    6.  
    7.     public float speed = 0.5f;
    8.     public Transform player;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         player = GameObject.FindGameObjectWithTag ("Player").transform;
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.         Vector3 displacement = player.position -transform.position;
    18.         displacement = displacement.normalized;
    19.         if (Vector2.Distance (player.position, transform.position) > 1.0f) {
    20.             transform.position += (displacement * speed * Time.deltaTime);
    21.         }
    22.  
    23.     }
    24. }
    Thanks for your understanding, and I wish to all of you a pleasant day.

    v1ruso
     
  2. frasderp

    frasderp

    Joined:
    Oct 6, 2016
    Posts:
    19
    Can you add a print function to your onTriggerEvent to confirm that it is being triggered by the Player moving through it?
     
  3. v1ruso

    v1ruso

    Joined:
    Aug 21, 2017
    Posts:
    4
    Thanks for your answer.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class WallToFlip : MonoBehaviour {
    5.     public Animator lionanimation;
    6.     public bool vraifaux;
    7.     // Use this for initialization
    8.     void Start () {
    9.         lionanimation = GameObject.FindGameObjectWithTag ("lion").GetComponent<Animator> ();
    10.         vraifaux = false;
    11.     }
    12.     void OnTriggerEnter2D(Collider2D other)
    13.     {
    14.         if (other.tag == "Player")
    15.         {
    16.         Debug.Log ("Detected");
    17.             vraifaux = !vraifaux;
    18.         }
    19.     }
    20.     void Update(){
    21.         lionanimation.SetBool ("facingRight", vraifaux);
    22.     }
    23. }
    24.  
    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
     
  4. v1ruso

    v1ruso

    Joined:
    Aug 21, 2017
    Posts:
    4
    Okay I understood where was my mistake, i put a box collider above the lion, and not a box collider 2d...
     
  5. frasderp

    frasderp

    Joined:
    Oct 6, 2016
    Posts:
    19
    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).
     
  6. v1ruso

    v1ruso

    Joined:
    Aug 21, 2017
    Posts:
    4
    Thank you very much ;)