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 $$anonymous$$ · Jan 31, 2014 at 04:11 PM · coroutineyieldwaitforseconds

lock a target after a few seconds

I like to lock a target based on Raycasthit, but it will change a 'tag' from the object AFTER a few seconds... if the RaycastHit exit, before 5 seconds, the 'tag' is not changed.

I have this code:

 function Update () {
 
     var foundHit : boolean = false;
     var hit : RaycastHit;
 
         var fwd =  transform.TransformDirection (Vector3.forward);
         foundHit = Physics.Raycast(transform.position, fwd, hit);
     
 
     if (foundHit && hit.transform.tag != tagCheck) //&& !checkAllTags
         foundHit = false;
 
     if (foundHit)
     {
             // here is my problem. i need 5 seconds before execute....
             yield WaitForSeconds (5);   
              // ...the next line
         hit.collider.transform.tag = "lockEnemy";
         print ("Mudeia a tag para = lockEnemy");
     }
 }

But, the line:

 yield WaitForSeconds (5);

needs a coroutine to set properly.. so how i can do this to work?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jan 31, 2014 at 11:35 PM

You have a few problems here. The major one is that you cannot yield inside of Update(). So you would need to create a coroutine to use yield. As an alternate you can use what I like to call a 'timestamp'. I'm assuming what you want to happen is that the player needs to have an object in front of this game object continuously for 5 seconds in order to change the tag to 'lockEnemy'. Here is some alternate code:

 #pragma strict
 
 var tagCheck = "Target";
 var lockTime = 5.0;
 
 private var locking = false;
 private var timestamp = 0.0;
 private var hit : RaycastHit;
 
 function Update () {
      
     if (Physics.Raycast(transform.position, transform.forward, hit) && (hit.transform.tag == tagCheck)) {
         if (!locking) {
             locking = true;
             timestamp = Time.time + lockTime;
         }
     }
     else {
         locking = false;
     }
 
     if (locking && Time.time >= timestamp) {
        hit.collider.transform.tag = "lockEnemy";
        print ("Mudeia a tag para = lockEnemy");
     }
 }
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 $$anonymous$$ · Feb 01, 2014 at 03:03 AM 0
Share

Perfect.. exactly what i need.. a few seconds before change the 'tag' . One more question: If i want that the new 'lockEnemy' stay with this tag for 8 seconds... even if the raycast is not 'hit' anymore. and after 8 seconds, the other object back to set 'enemy' on a tag (with is equal to tagCheck)? Thanks man!

avatar image $$anonymous$$ · Feb 01, 2014 at 03:05 AM 0
Share

Whooo nice... i put this other js on another object:

 function Update () {
 
     if(this.transform.tag == "lockEnemy") {
     Espera ();
     }
 
 }
 
 function Espera ()
 {
     yield WaitForSeconds (10);
     this.transform.tag = "enemy";
     print ("$$anonymous$$udei a tag para = " + this.transform.tag);
 }

works.

avatar image $$anonymous$$ · Feb 01, 2014 at 03:46 AM 0
Share

i change.. i used the 'timestamp' principle and change to this:

 #pragma strict
 
 var tagCheck = "lockEnemy";
 var lockTime = 8.0;
 
 private var timestamp = 0.0;
 private var mutavel = false;
 
 
 function Update () {
 
     if (this.transform.tag == tagCheck) {
         if (!mutavel) {
         mutavel = true;
         timestamp = Time.time + lockTime;
 
         }
     } else {
         mutavel = false;
     }
     if (mutavel && Time.time >= timestamp) {
         transform.tag = "enemy";
         print ("Voltei a ser: " + this.transform.tag);
     }
 
 }

is much more efficient in terms of processing. 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

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

What is wrong with this use of WaitForSeconds? 1 Answer

Alternative to "yield return WaitForSeconds()" in Coroutine with .NET Framework (Unity 2018.4) 0 Answers

Coroutine sequence not running properly 1 Answer

Getting an error when trying to yield 2 Answers

yield works, but yield WaitForSeconds does not? 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