I was wondering, how do I make a lightsoure turn off, if I interact with, say, a light switch?
Or, another example (Which is really what I’m trying to do):
My player is walking down a corridor, when, all of a sudden, the lights in the corridor should turn off by themselves.
I was thinking something a, “when the player collides which this part of the floor: turn the lights off”-script?
Actually, if you could just make a quick number of point I hav to go through to make this work
it woul dbe absolutely great. If not, still, thanks so much.
That’s about it really. Here’s a code snippet in C#, don’t know if works as I haven’t tested it.
When something enters, you check for a tag(in this example) and if it matches, you turn off the light.
For a cooler effect you could make lights flicker before they turn off, but that’s unrelated.
using UnityEngine;
using System.Collections;
public class Code : MonoBehaviour {
public Light lightToDisable;
void OnTriggerEnter(Collider o)
{
if (o.tag == "Player")
{
lightToDisable.enabled = false;
}
}
}
var lightToDisable : Light;
function OnCollisionEnter(collision : Collision) {
if(collision.tag == "Player"){
print("Collision!");
lightToDisable.enabled = false;
}
}
I can’t see why it doesn’t work. It seems like the problem is that there never is a collision between my player and the floor, on which this script is attached
The
print("Collision!");
never prints that there is a collision, so that might be the issue.