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 /
  • Help Room /
avatar image
0
Question by CrazyNinja2000 · Nov 07, 2015 at 03:13 PM · delay

Delaying between statements

I am working on an animation that rotates an object by 60 degrees. I was thinking of delaying the rotation every 10 degrees by 0.25 seconds. All I need is a delay function between statements. When ever I try to find this answer, people talk about coroutines, yield waitforseconds, but nothing works and can go between statements. How can I simply delay it for a certain amount of time between statements? My Code:

 void Move(){
         Vector3 v31 = new Vector3 (0, 0, 0);
         Vector3 v32 = new Vector3 (0, 0, 3);
         tf.RotateAround(v31, v32, 10);
         //delay
         tf.RotateAround(v31, v32, 10);
         //delay
         tf.RotateAround(v31, v32, 10);
         //delay
         tf.RotateAround(v31, v32, 10);
         //delay
         tf.RotateAround(v31, v32, 10);
         //delay
         tf.RotateAround(v31, v32, 10);
     }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by NightLucididty · Nov 07, 2015 at 03:58 PM

If I were you I would just add a little timer, here ill give you an example

 float timeInterval = 0.25f; //Or whatever time interval you want
 float timer = 0;
      void Start() {
          timer = timeInterval;
      }

  void Update() {
      timer -= Time.deltaTime;
  }

 void Move(){
          Vector3 v31 = new Vector3 (0, 0, 0);
          Vector3 v32 = new Vector3 (0, 0, 3);
          tf.RotateAround(v31, v32, 10);
          if(timer < 0) {
              timer = timeInterval;
              tf.RotateAround(v31, v32, 10);
          }
         if(timer < 0) {
              timer = timeInterval;
              tf.RotateAround(v31, v32, 10);
          }
          if(timer < 0) {
              timer = timeInterval;
              tf.RotateAround(v31, v32, 10);
          }
          if(timer < 0) {
              timer = timeInterval;
              tf.RotateAround(v31, v32, 10);
          }
         if(timer < 0) {
              timer = timeInterval;
              tf.RotateAround(v31, v32, 10);
          }
      }

Thats the simplest way you could do that, if you want a more detailed one just ask and ill see what I can do Good luck NightLucidity

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

Answer by 12boulla · Nov 07, 2015 at 04:03 PM

I would use InvokeRepeating to achieve this:

 int numOfTimes = 6; //The number of times to rotate by 10
 
 void Start(){
          InvokeRepeating("Move", 0.25f,0.25f); //This calls Move every 0.25 seconds. This can be called from wherever you want.
      }
 
 void Move(){
     if(numOfTimes != 0)
     {
         Vector3 v31 = new Vector3 (0, 0, 0);
         Vector3 v32 = new Vector3 (0, 0, 3);
         tf.RotateAround(v31, v32, 10);
         numOfTimes -= 1;
     }
     else{
         CancelInvoke("Move"); //Stops Move from being called evey 0,25 seconds
     }
 }

NOTE: Not tested...

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 CrazyNinja2000 · Nov 07, 2015 at 04:25 PM 0
Share

But I can only call this one time, which is the start. I need to be able to call $$anonymous$$ove when ever I need to.

avatar image NightLucididty CrazyNinja2000 · Nov 08, 2015 at 01:28 PM 0
Share

@CrazyNinja2000 Does $$anonymous$$e work? If not why?

avatar image
0

Answer by JoshuaMcKenzie · Nov 08, 2015 at 02:34 PM

Aside from using Mecanim to animate this, I would agree that Coroutines are your best bet. you are possibly having trouble getting Coroutines working as you might be attempting to call them in a single function. Coroutines operate best as a standalone function that must have the return type IEnumerator to work correctly.

here is an example code snippet of your situation using coroutines

     Vector3 v31 = new Vector3 (0, 0, 0);
     Vector3 v32 = new Vector3 (0, 0, 3);
     Coroutine handle;
     
     public void Move()
     {
         CancelMove(); //if already moving, stop current coroutine so you could restart it
         handle = StartCoroutine(MoveRoutine());
     }
     void CancelMove()
     {
         if (handle != null)
         {
             StopCoroutine(handle);
         }
     }
     IEnumerator MoveRoutine()
     {
         for (int iter=0; iter<6; iter++)
         {
             transform.RotateAround(v31, v32, 10);
             yield return new WaitForSeconds(0.25f);
         }
     }

You can publically (or privately, all matter how you want to access this) just call Move() and the rest is handled internally.

It is easy for someone to misunderstand how Coroutines can work for someone who has never worked with a similar concept before as they are a tad different from how a normal function operates. When you reach a "Yield" in a coroutine the compiler simply stops and does the rest of the function later after a specific trigger has been met (waiting for a number of seconds, for a different coroutine to finish or even on the next frame). so when you reach that Yield statment in the MoveRoutine the function is basically placed on standby for 0.25 seconds before it goes through the next iteration of the for loop. In the meantime the compiler is free to do other things while the coroutine/function is in its "micro-hibernation"

CancelMove() and the variable "handle" aren't really needed to get this to work but grant you the power to cancel the coroutine if you ever needed to.

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

33 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

Related Questions

[Lots of Pictures] Idle to Run Animation Delay? When Pressing Forward Slides forward without Animation 0 Answers

How to make pause beetwen action 0 Answers

How to Remove Delay in Button Click to Animation 0 Answers

Making button function wait for function before it to finish / Using WaitForSeconds. 0 Answers

my game freeze when arriving at a specific level at the first trigger 0 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