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 h4wkeye · Mar 29, 2011 at 11:41 AM · lightswitch

Spotlight switch script?

Hello fellow game devs! :)

i'm creating a showroom and i have made 4 spotlights tilted in the rifght angle for illuminating the presented object.

I need to write a script where the user is able to switch each of the 4 spots on and off.

i am relatively newb if it comes to manipulating game objects...do you guys have any advice?

thanks in advance

h4wkeye

EDIT: The User is walking through the showroom using a standard FPS prefab.

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 Statement · Mar 29, 2011 at 12:29 PM

You could make an Interacter script that you add to your camera that send OnInteract on any ray hit. Then you can easily make a switch for your light(s). Just add a collider to the switch and set up target, range, proper layer masks and you're ready to go. This can be used to do all sorts of interactions.

Interacter.js (Place on Camera)

var interactLayers : LayerMask = -1; var interactRange : float = 2.0f;

function Update() { if (Input.GetKeyDown(KeyCode.E)) { var hit : RaycastHit; if (Physics.Raycast (transform.position, transform.forward, hit, interactRange, interactLayers)) hit.collider.SendMessage("OnInteract", gameObject, SendMessageOptions.DontRequireReceiver); } }

LightSwitch.js (Place on button that has collider)

var target : Light;

function OnInteract() { target.enabled = !target.enabled; }

Step by step tutorial:

  1. Create a new Scene and delete the default "Main Camera".
  2. Create a new Cube and call it "Floor". Scale it so it becomes your floor.
  3. Drag in "First Person Controller" prefab into your scene and place it on your floor.
    • Set layer to "Ignore Raycast" on your "First Person Controller" object (And all children).
    • Add Interacter script to "First Person Controller/Main Camera". Click Continue.
    • Set Interacter.interactLayers to every layer except "Ignore Raycast".
  4. Create a new Light and call it "Light". Place it somewhere you can see its light.
  5. Create a new Cube and call it "Button". Place it somewhere you can reach it.
    • Add LightSwitch to "Button" and set its target to "Light".
  6. Play the game. Walk up to the button and press E. It should now toggle off and on.

I did just this and it works fine here.

Comment
Add comment · Show 9 · 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 h4wkeye · Mar 29, 2011 at 12:34 PM 1
Share

i freaking love you.

avatar image Statement · Mar 29, 2011 at 12:40 PM 0
Share

Aww, thanks mate.

avatar image Statement · Mar 29, 2011 at 12:42 PM 0
Share

If you want to know the gameObject which called OnInteract, you can just change the signature of the function to function OnInteract(interacter : GameObject) { }. Note that this would return the gameObject of the camera in your case.

avatar image h4wkeye · Mar 29, 2011 at 12:45 PM 0
Share

I cannot get it to work i gave the cube a raycast collider and checked "is trigger" then i attached the script to the cube it wont work D:

avatar image Statement · Mar 29, 2011 at 12:50 PM 0
Share

It's probably because the camera hit the character capsule. I had this problem too. You need to adjust the layer mask so it doesn't include the character. You could make this easy by creating a new layer mask for your character and have the interacter ignore it. That's the primary reason I added the variable interactLayers.

Show more comments
avatar image
0
Best Answer

Answer by Uzquiano · Mar 29, 2011 at 12:05 PM

Hi,

Have you take a look to the documentation?

Just put the intensity to 0 in the update()

light.intensity = amplitude;

Do you want to activate and desactivate the light when the player is around? Just add a trigger event with a collider, there are many examples in here if you do a search for colliders. Or in the documentation, for examlple here.

Hope this help you

Comment
Add comment · Show 2 · 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 h4wkeye · Mar 29, 2011 at 12:17 PM 0
Share

i've found a better solution for my problem. thank you anyway man :)

avatar image Statement · Mar 29, 2011 at 12:34 PM 1
Share

It would be helpful if you told us about your solution. This is a collaborative community :)

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

Making A Script for my Switch 1 Answer

Is there a way to measure light exposure for a specific point in the game world that can be used in scripts? 2 Answers

Switch between two light sources 3 Answers

Light rarely turns on 1 Answer

Switching GUI on and off 2 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