- Home /
How to detect sprite in player's position (2D Platformer Game) And get it t do something
Hi. I am a beginner in Unity and programming. I have made a very basic 2D platformer game using Unity's Standard Assets and by following an online youtube course. Now I wanted to implement a feature in which when a player presses the shift key on their keyboard, while standing in front of a light-post, the light turns off. So, I just wanted to ask how i should change the image to show an off-light-bulb instead of a switched on one. Also, how can I detect if the Player is standing in front of a light-post?? Any help is greatly appreciated.
Answer by TanselAltinel · Apr 23, 2018 at 12:19 AM
- Detecting player in front of a lamp post: 
Put a BoxCollider2D on lamp post, set it to trigger, and put a script on the lamp with OnTriggerEnter2D function. When player enters the trigger area, that function will be called.
- On and Off sprites changing 
Changing sprite in runtime is quite easy. Just put a variable in your script:
 public Sprite lampOn;
 public Sprite lampOff;
Then when you want to change the sprite, you can easily assign it:
 // to make it on
 GetComponent<SpriteRenderer>().sprite = lampOn;
 // to make it off
 GetComponent<SpriteRenderer>().sprite = lampOff;
will public sprite lampon/lampoff assign itself to any sprite with the same name fom the assets?
You can drag sprite to editor inspector field.
or do i have to do public Sprite lampon = Resources.Load<Sprite>("lamp_on");
Answer by Spacejet13 · Apr 25, 2018 at 12:36 AM
Well, I ran into a problem. As you might not have known, I have multiple of these lamps all throughout the level.(by the way, the trigger was working, but abruptly stopped after i don't know what I did, so I used an alternative method) I was constantly running into a problem in which all the lamps turned off when I wanted ,i.e, I cannot switch off a lone lamp, as all of them switch off instantly! This is my current code.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityStandardAssets._2D;
 
 public class toggleLamp : MonoBehaviour {
 
     public Sprite lampOn;
     public Sprite lampOff;
 
     private Transform lampT;
     private Transform playerT;
     private int keeper;
     private int score;
 
     void Awake () {
         lampT = transform;
         playerT = Platformer2DUserControl.playerT;
         score = 0;
         keeper = 0;
     }
 
     void Update () {
         if (Input.GetKeyDown (KeyCode.LeftShift) || Input.GetKeyDown (KeyCode.RightShift)) {
             if (keeper == 0) {
                 this.GetComponent<SpriteRenderer> ().sprite = lampOff;
                 score++;
                 keeper = 1;
                 Debug.Log (score);
             }
         }
     }
 }
how can I make it so that only the lamp I am standing in front of switches off?? :|
Could you share the code you are using the instantiate the copies of the lamp prefab? Perhaps you're doing something a bit funky there.
nvm, I found why it was not working. Thanks. I'll share my working code later.
It actually is just OnTriggerStay2D that I used ins$$anonymous$$d of OnTriggerEnter2D :)
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                