• 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 hotozi · Aug 12, 2012 at 01:18 AM · animationrotationloop

Scripting: make thing repeating

im making a door (simple door) and i want to make a rotation animation with the script...i want some thing on the door rotate and keep rotating...i also need it for other things but for now... this is my exemple

 transform.Rotate(0,-900*Time.deltaTime,0);
 
 yield WaitForSeconds(0.05);
 
 transform.Rotate(0,-900*Time.deltaTime,0);
 
 yield WaitForSeconds(0.05);
 
 transform.Rotate(0,-900*Time.deltaTime,0);
 
 yield WaitForSeconds(0.05);
 
 transform.Rotate(0,-900*Time.deltaTime,0);
 
 yield WaitForSeconds(0.05);
 
 transform.Rotate(0,-900*Time.deltaTime,0);
 
 yield WaitForSeconds(0.05);

supose that i want to repeat this 100 undread times...is there a way just write *100 and it keep repeating 100 times?

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 Bunny83 · Aug 12, 2012 at 01:22 AM 0
Share

That whole snippet doesn't make any sense ;) deltaTime is used when you do something every frame to correct framerate fluctuations. Using it anywhere else, especially when it's already time based will make things even worse.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Aug 12, 2012 at 01:32 AM

I guess you want to rotate your door by a certain amount of degree over a certain timespan.

Usually you would work with a target rotation you want to reach. This can be "lerped" (linearly interpolated) over time.

You can still use your approach above, but that low WaitForSeconds values might get inaccurate at lower framerates.

This would rotate the object relatively by -90°

 function RotateDoor()
 {
     for (var i = 0; i < 100; i++)
     {
        transform.Rotate(0,-0.9,0);
         yield WaitForSeconds(0.05);
     }
 }

when you call that function the door will rotate -90 in around 5 seconds. WaitForSeconds accumulated in a loop will not be very accurate in the end. Most the time it will take longer than you intended.

A better way is to do something like that:

 function RotateDoor(rotate : Vector3, time : float)
 {
     var initialRot = transform.localRotation;
     var targetRot = transform.localRotation * Quaternion.Euler(rotate);
     var t = 0.0
     while (t < 1.0)
     {
         transform.localRotation = Quaternion.Slerp(initialRot, targetRot, t);
         t += Time.deltaTime / time;
         yield;
     }
 }

This coroutine will perform any relative rotations within the given time.
Some examples:

 // Rotates around y axis by -90° in 2.5 seconds
 RotateDoor(Vector3(0, -90, 0), 2.5);

 // Rotates around y axis by 90° in 1 seconds
 RotateDoor(Vector3(0, 90, 0), 1.0);

 // Rotates around x axis by 45° in half a seconds
 RotateDoor(Vector3(45, 0, 0), 0.5);
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 Voltran · Aug 23, 2014 at 10:03 PM

can you guys explain these a little bit more ? I need to open and close my doors too, but i really did not find a how to for it. :( All i need is to open the doors just like in counter strike so that when i walk around in my building model i can enter the rooms. That's all i want and it should not be that complex...

Comment
Add comment · Show 1 · 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 hotozi · Nov 14, 2014 at 05:48 PM 0
Share

you know it is better to use (animator)animation for this... because cutting time seconds some time slow the system...ya i did experiment it....some time it gets lagy... use animation or a trick that i use some time

 door.localEulerAngles.y=$$anonymous$$athf.Lerp(door.localEulerAngles.y,180,5*Time.deltaTime);
avatar image
0

Answer by rick_rick · Jan 06, 2016 at 09:23 AM

You might try using colliders on the door and your first person camera to trigger an animation of the door opening. I'm assuming you're using a first person camera if you're doing a walk through of an environment.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 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

Related Questions

Problem with animation: they always start at the same rotation!! 1 Answer

Animate a cube rotation with script only 3 Answers

Rolling cubes one space and one rotation at a time, with a delay 2 Answers

Animation loop stop? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 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