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
0
Question by miniman3030 · Jul 30, 2013 at 02:34 AM · raycastlightrespawn

How to make a raycast that kills my player

In my game, I have lights that move back and forth and I want to make it so that when my player walks into the light, he dies and respawns. I was considering using a raycast to do this. How would i make this raycast so that it moves along with the lights and kills my player when he walks into it. I am also open to any other suggestions on how to kill my player when he walks into the light. Thanks.

Comment
Add comment · Show 5
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 Benproductions1 · Jul 30, 2013 at 02:38 AM 1
Share

A raycast is an action. It has no physical form and therefore cannot move. It's like saying I move the slashing of my knife. For your purpose I suggest a lineast, since it will make it easier when it comes to from/to calculation

avatar image ofishpega · Jul 30, 2013 at 02:40 AM 0
Share

you would have to send message from raycasting lazer script to the player script telling it to destroy.

avatar image clunk47 · Jul 30, 2013 at 02:46 AM 1
Share

It IS possible to do this with Raycast, and it's quite easy. But this would be a straight line, and would not work if you're wanting to use something like a spotlight with a wide angle, unless you do many raycasts within the light in a cone shape. But if you're talking something like a laser beam, this would work like so. Attach an empty gameObject as a child to your "light".

 RaycastHit hit;
 
 void Update()
 {
     if(Physics.Raycast(transform.position, transform.forward, out hit, $$anonymous$$athf.Infinity)
     {
         if(hit.collider.tag == "Player")
         {
             //Destroy and Respawn Code
         }
     }
 }

This is a very basic C# example.

If you want to do something like a spotlight, the EASIEST thing I can think of would be to use a 3D model of a cone or cylinder with a collider attached that "Is Trigger". Then you could look into OnTriggerEnter on the Unity Script Reference, it would work something like:

 void OnTriggerEnter(Collider collider)
 {
     if(collider.gameObject.tag == "Player")
         //Do something
 }

Question is a bit vague, so if this doesn't help at all, I would need some more detailed information.

avatar image Benproductions1 clunk47 · Jul 30, 2013 at 02:49 AM 3
Share

Or you could just test wether the player is within the "cone" of the light, eg within the radius, then use the raycast only to deter$$anonymous$$e the shadow. Like @robertbu XD

avatar image clunk47 · Aug 12, 2013 at 09:24 PM 1
Share

Please either accept an answer if one provides a solution, or close the question if you have found another solution. If not, please let us know this has not been resolved, so that we can work with you to figure it out. When you ask a question, we all put some real effort into answering it properly for you. Please have the courtesy to show some type of appreciation for the help you have received.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Jul 30, 2013 at 03:09 AM

Here is a method. Attach this script to a light:

 #pragma strict
 
 private var player : Transform;
 
 function Start() {
     player = GameObject.Find("Player").transform;
 }
 
 function Update() {
     var v3 = player.position - transform.position;
     var angle = Vector3.Angle(transform.forward, v3);
     
     if (angle <= light.spotAngle / 2.0 && v3.magnitude < light.range) {
         if (!Physics.Linecast(player.position, transform.position)) {
             player.position = Random.insideUnitSphere * 5.0;
         }
     }
 }

This assumes that your light is a spot light. You can take the angle check out for a point light. It works by checking to make sure the pivot point of the character is in the cone of light and that there are no objects between the light and the character's pivot point. This is basic functionality. A more through check would be to check all the points on the bounding box of the character. Note this script just moves the player. If you kill and then Instantiate a new player, you will need to reinitialize the 'player' variable.

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

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

18 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 avatar image

Related Questions

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

How do I get my player to die when light shines on him? 2 Answers

OnTriggerEnter forcefield help! 1 Answer

Raycasting to turn varible on and OFF 1 Answer

Finding Distance between Angles and Points 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