• 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 Xzapo · Jun 09, 2015 at 02:59 PM · c#raycasttimerdelete

Delete Object "Lock On"

So I am currently using RayCasting to detect and delete objects in my scene with a tag "found"(it's a stand still scavenger hunt game). What I am trying to do is make it so the player has to stare at the object for X amount of time like 3 seconds or so before the object deletes. Currently it's set up so that as soon as someone goes by it the object deletes. I've tried doing pausing an if statement and doing some weird things but that just totally broke it! Does any one have any tips on how to do this? This is what I've currently worked with...

 if (Physics.Raycast (new Ray (CameraFacing.transform.position, CameraFacing.transform.rotation * Vector3.forward), out hit)) {
             distance = hit.distance;
             //if(hit.collider.gameObject.tag == "found"){ 
                 
                 if(hit.collider.gameObject.tag == "found"){
                     //yield return new WaitForSeconds (waitTime);
                     foundObject = hit.collider.gameObject; 
                     Destroy(foundObject);
                     //x++;
                     //print(x);
                 }
             //} 
         } else {
             distance = CameraFacing.farClipPlane * 0.95f;
         }
         transform.position = CameraFacing.transform.position + CameraFacing.transform.rotation * Vector3.forward * distance;
         transform.LookAt (CameraFacing.transform.position);
         transform.Rotate (0.0f, 180.0f, 0.0f); 
     }
Comment
Add comment · Show 1
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 Gardes · Jun 10, 2015 at 12:39 AM 0
Share

IEnumerator is what you are looking for!

http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Gnemlock · Jun 10, 2015 at 02:31 AM

There are two ways I would do it. Pseudo below:

Countdown Approach

ou will have this running easier with a simple countdown on the object. Player looks at object, object counts to 3, object confirms player is still looking at object. Your only issue here is that if the player looks away, and just happens to be looking back at the end of that 3 seconds, your going to receive a false positive.

Player

 Update()
 {
     if(raycast(eyePosition, eyes.forward, out ray))
     {
         if(out.tag == "object")
         {
             object.SendMessage("ICanSeeYou");
         }
     }
 }

Objects

 IEnumerator ICanSeeYou()
 {
     yield return new WaitForSeconds(3.0f);

     if(raycast(playerEyePosition, playerEyes.forward))
     {
         // player is looking at the object
     }
 }



Constant Tick Approach

n this method, the player confirms it is looking at the object every update. Then, at every late update (guaranteed to run after the update cycles), the object confirms it is being looked at and increments its counter. This way, the object confirms it is being looked at for 3 seconds. That said, the object is not only constantly checking and having to account for more variables, but ever object will have to process at each late update. This is going to be more reliable in ensuring the rules of the game remain intact, but will be less efficient, the more objects you have in your scene.

Player

 Update()
 {
     if(raycast(eyePosition, eyes.forward, out ray))
     {
         if(out.tag == "object")
         {
             object.SendMessage("ICanSeeYou");
         }
     }
 }

Objects

 bool spied = false;    // is the object being looked at this frame?
 float spyTime = 0; // how long has the object been looked at for?
 float spyStart; // what time did we start looking at the object?
 
 void LateUpdate()
 {
     if(spied)
     {
         if(spied == 0.0f)
         {
             spyTime = time.time - spyStart;
         }
         else
         {
             spyTime += time.deltaTime;
         
             if(spyTime >= 3.0f)
             {
                 // object has been looked at for 3 straight seconds
             }
     }
     else
     {
         spied;
         spyTime = 0.0f;
         spyStart = 0.0f;
     }

     spied = false;
 }

 void ICanSeeYou()
 {
     spied = true;
     if(spyTime == 0.0f)
     {
         spyStart = time.Time;
     }
 }

I have listed the LateUpdate function for the object. This works just like Update, but guarantees all Update functions have been processed. This guarantees we will always check the players sight, then the object. I would also only recommend using the spyStart variable if you want to be pedantic in measuring the three seconds. Time delta is going to be able to handle the measurement as accurately as possible, apart from the first frame. Since the player is going to trigger the object between update functions, and time delta measures the time between the current and last update, the first time increment is going to overshoot itself by a small amount.

Comment
Add comment · Show 3 · 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 Xzapo · Jun 10, 2015 at 07:55 PM 0
Share

I'm running into some issues with the constant tick approach one

avatar image Gnemlock · Jun 11, 2015 at 02:03 AM 0
Share

What problems were you having specifically? That method is more pseudo then the other due to my lack of experience with coroutines, but Ill check it out when I have time at home.

avatar image Xzapo · Jun 11, 2015 at 01:47 PM 0
Share

Seems that the timers are not working correctly and spyTime is not going off to trigger the rest of the events to delete the object.

     void ICanSeeYou()
     {
         if (spied != true) {
             //prevFoundObj = foundObject;
             spied = true;
             if (spyTime == 3.0f && hit.collider.gameObject.tag == "found") {
                 print ("narp");
                 spyStart = Time.time;
                 Destroy (foundObject);
             }
         }
     }
 
     void LateUpdate()
     {
 
         if (spied) {
             if (spyTime == 0.0f) {
                 spyTime = Time.time - spyStart; 
                 print ("spy Time");
                 print (spyTime);
             } else {
                 spyTime += Time.deltaTime; 
 
                 if (spyTime >= 3.0f && hit.collider.gameObject.tag == "found") {
                     print ("happening");
                     // object has been looked at for 3 straight seconds
                     Destroy (foundObject);
                 }
             }
         }
             else
             {
                 spied = false; 
                 spyTime = 0.0f;
                 spyStart = 0.0f;
             }
             
             spied = false;
     }

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

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to get the distance between two objects in feet/meter? 1 Answer

Mouse Click + Raycast + Colliders 2 Answers

Raycasting how to find position on end of ray float distance 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