Search Unity

2d sprite lamp to play a "light up" animation when i push button near it, struggling

Discussion in 'Scripting' started by ABeardedWonder, Jul 7, 2018.

  1. ABeardedWonder

    ABeardedWonder

    Joined:
    Oct 4, 2016
    Posts:
    8
    Hi all

    Code (CSharp):
    1. public class BossLight : MonoBehaviour {
    2.  
    3.  
    4.     Animator anim;
    5.  
    6.     // Use this for initialization
    7.     void Start()
    8.     {
    9.         anim = GetComponent<Animator>();
    10.     }
    11.  
    12.  
    13.     private void OnTriggerEnter2D(Collider2D collision)
    14.     {
    15.         if (gameObject.tag == "Player" && Input.GetButtonDown("joystickbutton0"))
    16.         {
    17.             anim.SetBool("TurnOn", true);
    18.         }
    19.  
    20.     }
    21.    
    22. }
    Its the first time im doing something not from following a tutorial and just using the Scripting API and what ive learnt already from animating my player sprite.

    I have two bools, one for TurnOn and one for On,
    in my animator window i have the Idle stance to turn on animation, and then the turn on animation to just on (the lamp has glowing eyes and flames on the top, the turn on animation is the whole thing, the On animation is just the flames flickering).

    It seems like it should be something simple, check if the player has teh player tag (i do) and if im in the collider (both lamp and player both have colliders set to trigger and they are in together when i am pushing A to get it to turn on).

    I was going to try to get it to TurnOn, and when that animation has ran its course to make the On bool true and just have the lights flickering, but for now i think i'll be happy just having one of them work. Im hoping/sure its just a simple thing ive messed up but thought a second set of eyes might help me spot it
     
  2. ABeardedWonder

    ABeardedWonder

    Joined:
    Oct 4, 2016
    Posts:
    8
    Fixed it.

    Reaslised i needed other.tag and collider2d other