Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Spacejet13 · Apr 22, 2018 at 08:19 PM · 2d-platformerbeginnerdetectionkeypressimplementation

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Spacejet13 · Apr 23, 2018 at 12:42 PM 0
Share

Thanks a lot!

avatar image Spacejet13 · Apr 23, 2018 at 12:52 PM 0
Share

will public sprite lampon/lampoff assign itself to any sprite with the same name fom the assets?

avatar image TanselAltinel Spacejet13 · Apr 23, 2018 at 01:48 PM 0
Share

You can drag sprite to editor inspector field.

avatar image Spacejet13 · Apr 23, 2018 at 01:01 PM 0
Share

or do i have to do public Sprite lampon = Resources.Load<Sprite>("lamp_on");

avatar image
0
Wiki

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);
             }
         }
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Spacejet13 · Apr 25, 2018 at 12:42 AM 0
Share

how can I make it so that only the lamp I am standing in front of switches off?? :|

avatar image NonsenseSynapse Spacejet13 · Apr 25, 2018 at 01:02 AM 0
Share

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.

avatar image Spacejet13 NonsenseSynapse · Apr 25, 2018 at 01:29 AM 0
Share

nvm, I found why it was not working. Thanks. I'll share my working code later.

avatar image Spacejet13 · May 09, 2018 at 09:59 PM 0
Share

It actually is just OnTriggerStay2D that I used ins$$anonymous$$d of OnTriggerEnter2D :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

92 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trouble with tilemaps as prefabs for 2D plattformer 1 Answer

"someValue = anotherValue > 0.1" What does this mean? 3 Answers

Should I script my own Physics2D Platformer Character Controller just like in the Unity's Live Session? 1 Answer

How can I make my player unable to jump up walls? 2 Answers

How to set a condition for not colliding with an object using RayCast2D ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges