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 Tayfe · Apr 14, 2015 at 06:11 PM · rotatetimefunctionseconds

Creating function that lasts for x seconds

Hey guys,

Not sure if I'm just stupid right now but I got a small problem. I want to create a function that lasts for x seconds.

In my case I want to call a function that rotates an object by x degrees. But I don't want the object to be rotated instantly (within 1 frame) but slowly. The process of the rotation should always last for 1 second. So this is what my script looks like:

 function Update ()
     {
     if(Input.GetKeyDown("r"))
         {
         RotateObject(90);
         }
     }
     
 function RotateObject (degrees : int)
     {
     transform.RotateAround(Vector3.zero, Vector3(0,0,1), degrees * Time.deltaTime);
     }

Has anybody an idea how I can make the function RotateObject last for 1 second?

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 wesleywh · Apr 14, 2015 at 07:55 PM

Hum... Well I am not sure about the exactly one second timing here but this can give you a good example of how I might do it. You can look for better ways of timing it if you would like:

 var timeCounter:float = 0.0f;
 var timeToRotate:float = 1.0f;
 function Update ()
      {
      if(Input.GetKeyDown("r"))
          {
             if(timeCounter < timeToRotate){ //Added this to your code to execute this script only if it is under 1 second time count.
                 RotateObject(90);
                 timeCounter += Time.deltaTime;
              }
          }
      }
      
  function RotateObject (degrees : int)
      {
        
      transform.RotateAround(Vector3.zero, Vector3(0,0,1), degrees * Time.deltaTime);
      }

You of course would have to decide how you would want to reset the variable timeCounter back to zero to count again if you wanted 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
avatar image
0

Answer by elijahpeckman · Apr 16, 2015 at 06:42 PM

I'd use a Coroutine. Coroutines are specifically designed to allow you to stop what you're doing in the middle of a frame and come back to it next frame. I'm really only familiar with coding in C# at the moment so I don't trust myself to write an example code, but I encourage you to check the documentation and tutorial:

http://docs.unity3d.com/Manual/Coroutines.html http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines

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 YoungDeveloper · Apr 16, 2015 at 09:06 PM

For non constant time events you should use coroutines. This should start you going. This will rotate the mesh infinitely.

 private var isRotating:boolean = false;
 
 function Update (){
     if(Input.GetKeyDown("r")){
         if(!isRotating){
             isRotating = true;
             RotateObject();
         }
     }
 }
 
 
 function RotateObject() {
     while(true){
         transform.RotateAround(Vector3.zero, Vector3(0,0,1), 90 * Time.deltaTime);
         yield;
     }
 }
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

21 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

Related Questions

LookAt becoming inaccurate over time 0 Answers

Time Function in Javascript? 1 Answer

CountDown Timer Help (Seconds problem) 2 Answers

Rotate object on the Y axis 90 degrees every 5 minutes? 1 Answer

make an event occur after so many seconds? 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