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 ImPvEspec · Mar 11, 2014 at 03:35 AM · rotationvector3eulerangles

How can i rotate this vector over time?

This is the first time I've rotated objects in this manner, and I'm wondering how to add an "over time" component to what i already have. Basically I Click the object and it rotates instantly, and I want it to happen over period of like 0.5 seconds for polish purposes.

 if(hit.collider.tag == "Pipe")  { 
     zRot -= 90;
     hit.transform.eulerAngles = Vector3(xRot, yRot, zRot);
 }



I've moved objects before using the code below, and am wondering if a similar method would be used for rotating.

 function Update () {
 endPoint = target.transform.position;
     transform.position = Vector3.Lerp(startPoint, endPoint, (Time.time - startTime) / duration);
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

Answer by fifthknotch · Mar 11, 2014 at 04:22 AM

The best method to get something to rotate the same amount over time is to use Time.deltaTime. This will force the object to keep its rotation frame rate independent:

http://docs.unity3d.com/Documentation/ScriptReference/Time-deltaTime.html

Use transform.Rotate to actually rotate the object. More on that can be found:

http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html

And here is a code I made. Fill in the variables to suit your game best. Click to start rotation. You can repeatedly click to keep the rotation going.

     #pragma strict
     var x : int;
     var y : int;
     var z : int;
     var maxRotateTime : float;
     private var rotating : boolean = false;
     private var counter : float;
     
     function Update () {
         if (Input.GetMouseButtonDown(0) && !rotating) {
             rotating = true;
         }
         if (rotating) {
             counter += Time.deltaTime;
             if (counter < maxRotateTime)
                 transform.Rotate(Vector3(x, y, z) * Time.deltaTime);
             else {
                 rotating = false;
                 counter = 0;
             }
         }
     }
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 DiligentGear · Mar 11, 2014 at 04:24 AM

Unfortunately, this might need you to dig into Coroutine. The official tutorial is quite informative:

http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines

     bool bIsRunning = false; // Check if this coroutine is already running
     Vector3 rotateAngle = new Vector3 (0f,0f,-90f); // Corresponding euler angle
     float rotateSpeed = 5f; // How fast you want object to rotate
 
     IEnumerator RotateByClick(RaycastHit hit,Vector3 toEuler,float rotateSpeed)
     { 
         if (!bIsRunning)
         {
             bIsRunning = true;
             Quaternion fromQua = hit.transform.rotation;
             Quaternion toQua = Quaternion.Euler(toEuler);
             // 'Translate' euler angle to a quaternion
             float deltaT = 0f;
 
             while (deltaT < 0.99f) 
             // The rotation is completed when deltaT reaches 1, 
             // you can adjust the fraction if it's not accurate enough.
             {
                 hit.transform.rotation = Quaternion.Slerp(fromQua,toQua,deltaT);
                 // This is the rotation version of Lerp
                 deltaT += Time.deltaTime * rotateSpeed;
                 yield return null;
             }
 
             bIsRunning = false;
         }
         yield return null;
     }
     
     // Then you can add this to your raycast function
     if (hit.collider.tag == "Pipe")
     {
         StartCoroutine(RotateByClick(hit,rotateAngle,rotateSpeed));
     }



Comment
Add comment · Show 5 · 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 ArkaneX · Mar 11, 2014 at 03:42 PM 0
Share

Why unfortunately? This sounds like coroutines are worst thing ever, while they're quite important for Unity program$$anonymous$$g.

Btw - I also suggest reading Unity Gems article on coroutines.

avatar image DiligentGear · Mar 11, 2014 at 05:32 PM 0
Share

Apparently he is a beginner and he might have trouble understanding the syntax and how it works.

And I never meant coroutines are bad. I just meant that he might learn something that overwhelms a beginner.

avatar image ImPvEspec · Mar 12, 2014 at 05:22 AM 0
Share

I'm also doing it in Unity JS so coroutines aren't needed i believe? Ins$$anonymous$$d using While(???)

avatar image ArkaneX · Mar 12, 2014 at 08:15 AM 0
Share

@DiligentGear - I believe after reading linked tutorials, he should be fine :)

@ImPvEspec - coroutines are language independent, so you can use them in JS as well.

You can of course use while, but calling it in Update will make it execute in current frame. So if you change some position, rotation etc. inside while, only last result will be relevant. That's why coroutines were introduced - they work in a different way, and can be executed across more than one frame. I really suggest reading through provided links, and experimenting with coroutines a bit.

avatar image fifthknotch · Mar 12, 2014 at 03:34 PM 0
Share

You cannot have a coroutine inside that update function. It produces and error. :)

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

23 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

Related Questions

How to limit the range of rotation of the world around my player. 0 Answers

Object's vectors are not moving with the object, what did i do wrong? 1 Answer

Add a Vector3 to the rotation of an obj and have it behave with logic! 0 Answers

Turret rotation on one axis problems 2 Answers

Euler Angles and Vectors 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