- Home /
How do I get my player to die when light shines on him?
In my game, I have lights that move back and forth and I cant figure out how to make it so that my pkayer dies and respawns when he steps into the light.
Answer by Paul A · Jul 29, 2013 at 10:56 PM
Why not raycast (Unity Script Reference)? You can start your raycast at the origin of your light, and either set its distance, or opt out if you don't want to limit it. If the raycast has a collision with your player object, then tell it to die. The raycast is just a line, so if you want to check an area rather than just a line, you can raycast several times.
Answer by aldonaletto · Jul 29, 2013 at 11:04 PM
If only certain lights can kill the player, you could create a cone in Blender or other 3D editor and import it to Unity, then use this mesh to create a trigger. Add or child this trigger to the killing light prefab and add to it a kinematic rigidbody, then use OnTriggerEnter to cause damage to the player with a code like this:
var damage: float = 10; // damage per second
private var victim: Collider;
function OnTriggerEnter(other: Collider){
if (other.tag == "Player"){ // if player entered light...
victim = other; // he becomes the victim
}
}
function OnTriggerExit(other: Collider){
if (other.tag == "Player"){
victim = null; // player exited light: no victim
}
}
function Update(){
if (victim){ // if there is a victim under the light...
// apply damage proportional to the exposure time
victim.SendMessage("ApplyDamage", damage * Time.deltaTime);
}
}
In the player (which must be tagged as "Player") script, create the ApplyDamage function:
var health: float = 100;
function ApplyDamage(damage: float){
health -= damage;
if (health <= 0){
// player is dead
}
}
@aldonaletto - What a great idea! In all the posts I've read about deter$$anonymous$$ing light falling on a player or enemy, this has never been suggested. Given a scene of fixed objects and a fixed lights, you could model the light mesh to match the light and shadow of the scene. This would allow your character/enemy to not take hits if they are hidden from the light.
$$anonymous$$y first though for this problem was to use Vector3.Angle() and Vector3.Distance() to deter$$anonymous$$e if the character was in the range of light, and then Raycast() to deter$$anonymous$$e if the light was blocked.
@robertbu: Good point! I forgot about the possibility of something blocking the light, thus for a moving spotlight your approach would be better. But for a more complex ambient, with several fixed lights and shadows, a well created mesh could solve the problem. Probably the illu$$anonymous$$ated space should actually be covered by several mesh triggers, since only convex meshes would work.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to make a raycast that kills my player 1 Answer
Getting an object to spawn on a set time 1 Answer
enemy hit and respawn 0 Answers
Born again object 1 Answer