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 Eli-Davis · Jun 11, 2011 at 06:22 PM · rotationloopyieldwaitforsecondsspin

Simple rotation script. (loop)

I feel stupid asking this, but i'm not sure how its done. I would like an object to rotate 180 degrees on it's x axis after waiting 3 seconds, and then repeat. But when I use this script, it just waits three seconds on load time, then continues spinning forever. Sorry for this stupid question

 function spin(){
     yield WaitForSeconds(3.0);
     transform.Rotate(0, 0, 180 * Time.deltaTime);
 }
 
 function Update(){
     spin();
 }

I guess I want the spin function to loop, but I don't know how.

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

Answer by aldonaletto · Jun 11, 2011 at 06:57 PM

That's the old fashioned way to do this:

 private var tCycle:float;
 
 function Update(){
   var t = Time.time;
   if (t>tCycle) tCycle = t+3;
   if (tCycle-t<=1){ // Spins during the last second
     transform.rotate(0,0,180*Time.deltaTime);
   }
 }

I'm sure yield could be a better alternative, but I just can't understand this %#@! thing!

Comment
Add comment · Show 6 · 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 aldonaletto · Jun 11, 2011 at 07:01 PM 0
Share

Oh, I forgot to initialize tCycle to zero at the beginning. I think the system does that, but $$anonymous$$urphy's Laws are implacable...

avatar image Eli-Davis · Jun 11, 2011 at 07:03 PM 0
Share

thank you very much. could you explain the first if statment that ended with ;? how does that work. The script works by the way

avatar image aldonaletto · Jun 11, 2011 at 07:19 PM 0
Share

tCycle always indicate the end of a 3 seconds cycle. Whenever Time.time is higher than tCycle, tCycle is reloaded with Time.time plus 3 seconds. Be aware that after some time the object may drift a little, stopping at a slight different angle due to errors accumulated at each cycle. If it happens, call for help here and we'll try to find some way to fix it.

avatar image Eli-Davis · Jun 11, 2011 at 10:21 PM 0
Share

Yes, the more the game lags, the more it seems to get the angle wrong, I'm not completely sure how to fix this, but an idea is to set the rotation to 0, 0, 0 after a certain amount of turns or idk. Something like that? Also, could I put this on my blog? I'l reference people to your profile for credit.

avatar image aldonaletto · Jun 11, 2011 at 11:06 PM 0
Share

I just can't edit my answer! Damn new UA!
Anyway, you can fix the problem changing the first if this way:

if (t>tCycle){
tCycle = t+3;
if (transform.localEulerAngles.z<90)
transform.localEulerAngles.z = 0;
else
transform.localEulerAngles.z = 180;
}

It forces the object to the ideal angle at the beginning of each cycle.
Feel free to put this solution in your blog - I'll be glad to share this with more people!

Show more comments
avatar image
1

Answer by Kith · Jun 11, 2011 at 06:54 PM

If I understand the question, what you want is to have the object finish a rotation of 180 degrees every 3 seconds? What your code is doing now is initially waiting for 1 second (your yield statement), and then spinning 180 degrees per second after that. I think what you want would look something like this.

 function spin() {
        transform.Rotate(0,0,60*Time.deltaTime)
 }
 
 function Update() {
       spin();
 }

What's happening here is that your rotating the object 60 degrees per second (which is the same thing as saying 180 degrees every 3 seconds).

Hope that's what you were looking for!

-Kith

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 Eli-Davis · Jun 11, 2011 at 06:59 PM 0
Share

I'm sorry I worded the question wrong, I re worded it now. but thank you for your answer, you awnsered it correctly wrong worded question

avatar image Kith · Jun 11, 2011 at 07:23 PM 0
Share

No worries lol. Aldonaletto's answer should work fine for what you're trying to do then.

avatar image
0

Answer by AlexeyBukin · Jan 17, 2019 at 11:03 PM

If you're just want a nice demo, then you can use this script:

 IEnumerator spin(){
     while(true){
         yield return new WaitForSeconds(2.0f);
         float timer = 0f;
         while(timer<1){
             transform.rotate(0,0,180*Time.deltaTime);
             timer = timer + Time.deltaTime;
             yield return null;
         }
     }
 }
 
 void Start(){
     StartCoroutine(spin());
 }

But, if you want precidion in rotation, some changes should be applied:

 IEnumerator spin(){
     while(true){
         yield return new WaitForSeconds(2.0f);
         float timer = 0f;
         Quaternion lastRotation = transform.rotation;
         while(timer<1){
             transform.rotate(0,0,180*Time.deltaTime);
             timer = timer + Time.deltaTime;
             yield return null;
         }
         transform.rotation = lastRotation;
         transform.rotate(0, 0, 180);
         }
 }
 
 void Start(){
     StartCoroutine(spin());
 }

Hope it helps!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Return to the point of rotation? 2 Answers

A delay/yield/wait function for function Update? 6 Answers

Definition of Script.function() depends on Script.function()... 3 Answers

Yield Not Working - While Loop 3 Answers

Loop animation with a wait in between 3 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