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 Peanut97 · Jul 24, 2013 at 08:37 PM · gameobjectraycastdestroyraycastinghit

Help destroying gameObject on RayCastHit?

Hey all,

This is my first time using raycasts and I'm a total noob at it. I'm sorry for rookiness, but please try to be patient as I've been trying to figure this out forever now. Haha! So here is what I am trying to figure out:

I am currently making a horror game and I want the player to constantly be haunted by a monster. I have a system set up where the monster generates in certain areas... The only problem is... I need the monster to disappear 3 seconds AFTER the raycast (from the players viewpoint) hit his collider. I am working on getting a ray projecting from the First Person Controller's camera at the moment so don't worry about that (unless it's necessary.)

Thank you so much! I've been working at this for a while and it's getting frustrating. So thank you so much for your help! It means lots! (Also, I'm willing to provide more information if needed.) Thanks again!

Comment
Add comment · Show 2
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 dorpeleg · Jul 25, 2013 at 06:02 AM 0
Share

You do need to provide more info.

All you are saying is "I need help raycasting"...

What part do you need help with? what part of raycasting don't you understand? did you already try writing code? etc...

avatar image clunk47 · Jul 25, 2013 at 07:01 AM 1
Share

Ok here's the scenario I think you are trying to get at. If you want it where you have to have the camera look directly at the monster and after 3 seconds he disappears, here is a C# example of how to do so. This is a REALLY basic example just to get you started. You can use this same approach if you're wanting to use the raycast on your player object ins$$anonymous$$d of the main camera. Just replace the camera's position and direction here with your player's.

 using UnityEngine;
 using System.Collections;
 
 public class Example : $$anonymous$$onoBehaviour
 {
     RaycastHit hit;
 
     void Update()
     {
         if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, $$anonymous$$athf.Infinity))
         {
             if(hit.collider.gameObject.name == "$$anonymous$$onster")
             {
                 Destroy(hit.collider.gameObject, 3);
             }
         }
     }
 }

1 Reply

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

Answer by Seth-Bergman · Jul 25, 2013 at 07:10 AM

ok, this is pretty simple.. first, get the raycast:

http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

 function Update () {

     var hit : RaycastHit;

     if (Physics.Raycast (transform.position, transform.forward, hit)) {

next, we want to see if what we hit is the monster. You can use the object's name or tag to do this easily:

      if(hit.collider.tag == "monster"){

then to destroy:

     Destroy(hit.collider.gameObject);

usually, if you are planning on re-spawning the monster immediately, it would be less costly to just not destroy him at all, but just move him to the new spawn point, as Instantiate() is a costly function.

here's the whole thing, I think this would work:

 function Update () {

     var hit : RaycastHit;

     if (Physics.Raycast (transform.position, transform.forward, hit)) {

                 if(hit.collider.tag == "monster"){

                     Destroy(hit.collider.gameObject, 3); 

                 }
             }
   }

One final tip, if the monster is meant to be destroyed once in view, this may not work as well as you hope, since the ray is cast directly from the center of the camera.. only centering the monster in view is likely to trigger a hit, not sure if that's what you want..

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 Seth-Bergman · Jul 25, 2013 at 07:23 AM 0
Share

alternately, if you wanted the monster to disappear 3 seconds after he enters view, you may wish to check out:

Renderer.isVisible

Renderer.OnBecameVisible

http://answers.unity3d.com/questions/8003/how-can-i-know-if-a-gameobject-is-seen-by-a-partic.html

(however, isVisible and OnBecameVisible will return true as soon as any part of the model enters the camera...)

avatar image Peanut97 · Jul 25, 2013 at 06:17 PM 1
Share

Thank you people so much! I'm giving "positive answers" to both of ya! Thanks!

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

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

raycast to determine pivot 1 Answer

Ray Casting - Trigger function 1 Answer

How to use raycast on generated objects. 0 Answers

how to select an object with raycasting 1 Answer


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