Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 RealCool_Guy · Aug 27, 2018 at 09:09 PM · coroutineupdatefunctionwaitforsecondsdelay

How to create a delay in a function being called in update?

I am having an issue trying to delay my code once a certain condition has been met. Basically I have a laser object that turns towards a max angle and once that angle is reached, it revolves back the other way. I want to be able to pause the laser as it hits the max angle for a certain amount of time, then continue with the rest of the code. My code is shown below

 public class LaserRotate : MonoBehaviour
 {
     [Header("XZ Plane Rotation")]
     public bool ContinuousRotation; //if laser should continue to rotate
     [Range(0,179)] //Anything greater or equal to 180 causes full revolution
     public float RotationAngle; // max total rotation angle from starting point in xz direction
     public float RotationSpeed; //How fast to rotate
 
     private Quaternion StartingRotation;
     private Quaternion TargetRotation;
     private float RelativeRotation; //signifies angle with respect to starting position
     private Vector3 AngleRotate; 
 
     private int direction = 1; //positive is clockwise,negative counter
     // Use this for initialization
     void Start ()
     {
         AngleRotate = new Vector3(0, direction * RotationAngle, 0);
         StartingRotation = transform.rotation;
         TargetRotation = Quaternion.AngleAxis(RotationAngle, AngleRotate); 
         RelativeRotation = transform.rotation.eulerAngles.y - StartingRotation.eulerAngles.y;
     }
     
     // Update is called once per frame
     void Update ()
     {
         LaserRotation();
     }
 
     public void LaserRotation()
     {
         AngleRotate = new Vector3(0, direction * RotationAngle, 0);
         TargetRotation = Quaternion.AngleAxis(RotationAngle, AngleRotate * direction); 
         RelativeRotation = (transform.rotation.eulerAngles.y - StartingRotation.eulerAngles.y);
 
         if (Mathf.Abs(RelativeRotation) > 180) 
         {
             RelativeRotation = 360 - Mathf.Abs(RelativeRotation);
         }
         //print("Relative Rotation angle is: " + RelativeRotation);
         //print("Target Rotation is: " + TargetRotation.eulerAngles.y);
         if (RelativeRotation >= TargetRotation.eulerAngles.y) 
         {                                                  
             direction = direction * -1;                     
             StartingRotation = transform.rotation; 
             print("Reversing direction...");
         }
         transform.Rotate(AngleRotate * Time.deltaTime * RotationSpeed);       
         
     }
  
 }

In the segment

 if (RelativeRotation >= TargetRotation.eulerAngles.y) 
          {                                                  
              direction = direction * -1;                     
              StartingRotation = transform.rotation; 
              print("Reversing direction...");
          }

I want to add a small delay, because this signifies when the max angle has been reached. I want the laser to stay there for about a second, then continue with the rest of the code.

I've tried using coroutines, but this does not prevent the update from completing. If I try putting the entire function in a repeating coroutine that I call from start, the laser just jumps from max position to beginning position.

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
Best Answer

Answer by RealCool_Guy · Aug 28, 2018 at 03:23 AM

 private void timer;
 private bool TimeStart;
 
 public void LaserRotation()
         {
             AngleRotate = new Vector3(0, direction * RotationAngle, 0);
             TargetRotation = Quaternion.AngleAxis(RotationAngle, AngleRotate * direction); 
             RelativeRotation = (transform.rotation.eulerAngles.y - StartingRotation.eulerAngles.y);
     
             if (Mathf.Abs(RelativeRotation) > 180) 
             {
                 RelativeRotation = 360 - Mathf.Abs(RelativeRotation);
             }
             //print("Relative Rotation angle is: " + RelativeRotation);
             //print("Target Rotation is: " + TargetRotation.eulerAngles.y);
             if (RelativeRotation >= TargetRotation.eulerAngles.y) 
             {                                                                                                              
                 TimeStart = true;
                 timer += Time.deltaTime;
                 if (timer > 1)
                 {
                     timer = 0;
                     direction = direction * -1;
                     StartingRotation = transform.rotation;            
                     print("Reversing direction...");
                     TimeStart = false;     
                 }              
             }    
             if(TimeStart == false)
             {
                 transform.Rotate(AngleRotate * Time.deltaTime * RotationSpeed);
             }
             else { }
         }

I like the idea, however the last line

transform.Rotate(AngleRotate Time.deltaTime RotationSpeed);

is the line that causes the laser to rotate. The code you provided prevents the laser from stopping at the max angle, instead it continues rotating beyond.

But your code gave me an idea, with only a little tweak it runs as intended for now. I added in a bool TimeStart. When the it reaches max angle its value is set to true, and the laser is no longer rotated. After 1 second has passed TimeStart is set to false and the laser can now rotate again. Why do most of the answers related to creating a Delay suggest a coroutine? I understand it has little to no overhead, but it seems like this method is quicker and easier. I still have no idea how I would implement this with a coroutine @Hellium

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 Hellium · Aug 28, 2018 at 05:14 AM 0
Share

$$anonymous$$aybe, if you set RelativeRotation to 0 inside if (RelativeRotation >= TargetRotation.eulerAngles.y) , you won't need the boolean.

In your case, I don't believe using a coroutine will be cleaner or simpler.

avatar image RealCool_Guy Hellium · Aug 28, 2018 at 04:57 PM 0
Share

Ah okay thank you for the suggestion. I think I've got my answer with your help

avatar image
1

Answer by Hellium · Aug 27, 2018 at 09:21 PM

Following code not tested

 private float timer = 0 ;
 
 public void LaserRotation()
 {
     AngleRotate = new Vector3(0, direction * RotationAngle, 0);
     TargetRotation = Quaternion.AngleAxis(RotationAngle, AngleRotate * direction); 
     RelativeRotation = (transform.rotation.eulerAngles.y - StartingRotation.eulerAngles.y);
 
     if (Mathf.Abs(RelativeRotation) > 180) 
     {
         RelativeRotation = 360 - Mathf.Abs(RelativeRotation);
     }
     //print("Relative Rotation angle is: " + RelativeRotation);
     //print("Target Rotation is: " + TargetRotation.eulerAngles.y);
     if (RelativeRotation >= TargetRotation.eulerAngles.y) 
     {                                                  
         timer += Time.deltaTime ;
         if (timer > 1) 
         {
             timer = 0 ;
             direction = -direction;                     
             StartingRotation = transform.rotation; 
             print("Reversing direction...");
         }
     }
     transform.Rotate(AngleRotate * Time.deltaTime * RotationSpeed);       
     
 }
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

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

Related Questions

General question about functions, cofunctions and wait timers 0 Answers

Coroutine not running after yield return new WaitForSeconds 3 Answers

Im a bit confused on a simple script 1 Answer

check bool in WaitForSeconds 2 Answers

C# simple delay execution without coroutine? 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