• 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 miniman3030 · Jul 29, 2013 at 10:44 PM · lightrespawnkill player

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.

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
0

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.

Comment
Add comment · 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
0

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 t$$anonymous$$s mesh to create a trigger. Add or c$$anonymous$$ld t$$anonymous$$s 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 t$$anonymous$$s:

 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 (w$$anonymous$$ch 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
   }
 }

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 robertbu · Jul 29, 2013 at 11:25 PM 0
Share

@aldonaletto - What a great idea! In all the posts I've read about determining 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.

My first though for this problem was to use Vector3.Angle() and Vector3.Distance() to determine if the character was in the range of light, and then Raycast() to determine if the light was blocked.

avatar image aldonaletto · Jul 30, 2013 at 01:22 AM 0
Share

@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 illuminated space should actually be covered by several mesh triggers, since only convex meshes would work.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a raycast that kills my player 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Enemy respawning after death 1 Answer

lighting click help 3 Answers

Can someone help me fix my Javascript for Flickering Light? 6 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