- Home /
Trouble with light switch
I know there are quite a few topics to do with light switches but I'm really having trouble getting this to work.. I have 3 objects; - sphere (player) - cube (wall) - light
I want to make it so when the player hits the wall, it switches on a light.. I'm trying to get all this done in JavaScript but because I'm still learning it seems to be giving me trouble.. any help would be great! ^_^ Thanks.
Can you describe what you have done and tried? You may want to check up Collier and the trigger functions in the documentation for starters
Answer by B3NN · May 27, 2010 at 12:57 AM
Well I finally got this thing working!
var linkedLight : Light; //linked to switched off light
function OnControllerColliderHit(triggerLight : ControllerColliderHit)
{
if(triggerLight.gameObject.tag == "wall") //"wall" is the tag on the wall I'm colliding with
{
linkedLight.enabled = true;
}
}
Sorry to bother everyone with the question.. but thanks a lot for the resources, it took me a while, but I finally figured it out :) Now I can add some variables to toggle the light back off again!
Answer by bowditch · May 26, 2010 at 03:33 PM
Make sure you have physics colliders on all of your GameObjects.
Then, make sure you enable the "Is Trigger" in the Wall GameObject.
You can choose from a number on OnCollision Functions to complete this.
Is this helpful? I can give you more advice if you have more details to share about what you want to accomplish.
You may also want to check out Tornado Twins - Understanding Triggers Video Tutorial
I have physics colliders on the sphere and the wall, I didn't think I would need one on the light because it's floating above the player. When I turned on "Is Trigger" for the Wall GameObject it made me run through it.. so I've made a new one just in front which acts as the trigger but doesn't get rendered, I think that way I can still collide with it even though it can't be seen.. The biggest problem I am having now is actually switching the light, I can't seem to find any way of making it work..
Answer by B3NN · May 27, 2010 at 12:20 AM
Turns out my light switch was fine.. I tried:
var linkedLight : Light; //this is liked to a light which is switched off
function Update ()
{
if (Input.GetKey("q"))
{
linkedLight.enabled = true;
}
}
This worked fine.. when I run the game and press "q" the light switches on.. so I guess my problem lies in the collision... I'm using:
function OnCollisionEnter(triggerLight : Collision)
{
if(triggerLight.gameObject.tag == "wall")
{
linkedLight.enabled = true;
}
}
Your answer
Follow this Question
Related Questions
Using shadows as Triggers? 1 Answer
Can't click gameobject when over another trigger? 1 Answer
Player detecting collision when crouching 1 Answer
Compound collider does not trigger the parent 1 Answer
Missing taps on triggers/collider 0 Answers