Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Martin 3 · Nov 26, 2010 at 05:43 PM · buttonfps

How to make a button in FPS

Hi, I have a very simple question. I want a light to turn on, when player object is close to a light switch, and the player presses "E". Can you give an example of a script to do that?

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

1 Reply

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

Answer by Anton S. · Nov 26, 2010 at 06:25 PM

You could attach a collider set as trigger to the light switch and add this code to a script at the light switch:

function OnTriggerStay (other : Collider) {
    if(other.parent.tag == "player" && Input.GetButtonDown("switch")){
        SwitchLightOn();
    }
}

It should call the function SwitchLightOn when the player comes into the trigger. You also have to set the tag of the player to player and define the button "switch" in the Input settings. I'm not sure if it'll work, i haven't tested it.

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 Martin 3 · Nov 26, 2010 at 08:27 PM 0
Share

Hm Havent tried it yet, but I guess the player have to collide with the button, so I guess its not okay to just stand close to it?

avatar image fireDude67 · Nov 26, 2010 at 09:17 PM 0
Share

All you would have to do is make the trigger collider a little bigger. Also to create a trigger for your light switch, create a cube/sphere , resize it until you have it the right size, then remove the mesh render and check 'Is Trigger' in collider settings

avatar image Martin 3 · Nov 26, 2010 at 10:07 PM 0
Share

Okay. It seems to work, but If I have two objects : Switch and the Lamp, how do I turn the spotlight from the lamp on from a script attached to the switch?

avatar image Anton S. · Nov 29, 2010 at 02:23 PM 0
Share

You could add a public variable of the type GameObject to the switch and assign the Lamp to that Variable (In the Unity Editor). In your script you can run GetComponent(Lamp) on the variable and change its intensity value.

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

No one has followed this question yet.

Related Questions

Click Once to play an entire animation 2 Answers

[Android] Why does my enemy die no matter where I am in the 3D environment? 1 Answer

Animate object on mouse button press 1 Answer

When my button is pressed the action reapeats 1 Answer

iOS Polygon button not working with yield WaitForSeconds 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