Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by $$anonymous$$ · Mar 19, 2015 at 10:29 PM · rotationtransformrotatetowardsover time

How can I rotate a Transform over time?

How can I rotate a Transform over time?

I have had a lot of trouble with this in the past. I wrote a script which rotates a Transform by a specified rotation over n seconds, or rotates a Transform towards a Vector3 over n seconds, in a one liner. I hope it helps others in the future. See below.

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

1 Reply

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

Answer by $$anonymous$$ · Mar 19, 2015 at 10:29 PM

Create a script called TransformExtentions. Paste the code into that script.

You can call Transform.RotateTowardsOverTime(Vector3, float) on your Transform where the Vector3 is the target you want to rotate towards and the float is the number of seconds you want it to take.

You can call Transform.RotateOverTime(Vector3, float) on your Transform where the Vector3 is the rotation you want to apply and the float is the number of seconds you want it to take. You can apply rotations greater than 360 degrees or less than -360 degrees.

Calling Transform.RotateTowardsOverTime(Vector3, float) or Transform.RotateOverTime(Vector3, float) will cancel out any active rotation calls on the Transform.

If you pass in 0 seconds, the Transform will rotate infinitely. If you pass in negative seconds, the Transform will rotate in reverse.

 using UnityEngine;
 using System.Collections;
 using System;
 
 public static class TransformExtensions
 {
     /// <summary>
     /// Rotates the transform to a specified rotation over a set number of seconds.
     /// For an infinite rotation, multiply the degrees by a float to adjust the speed, and set the duration to 0 seconds.
     /// Calling RotateOverTime() or RotateTowardsOverTime() will cancel any pending rotations on this transform.
     /// </summary>
     public static void RotateTowardsOverTime(this Transform transform, Vector3 degrees, float seconds)
     {
         Vector3 rotationToBeMade = degrees - transform.rotation.eulerAngles;
         if (degrees.z > 270.0F && transform.rotation.eulerAngles.z < 90.0F)
         {
             rotationToBeMade.z = -(360.0F - degrees.z + transform.rotation.eulerAngles.z);
         }
         if (transform.rotation.eulerAngles.z > 270.0F && degrees.z < 90.0F)
         {
             rotationToBeMade.z = 360.0F - transform.rotation.eulerAngles.z + degrees.z;
         }
         RotateOverTime(transform, rotationToBeMade, seconds);
     }
 
     /// <summary>
     /// Rotates the transform by a specified number of degrees over a set number of seconds.
     /// For an infinite rotation, multiply the degrees by a float to adjust the speed, and set the duration to 0 seconds.
     /// Calling RotateOverTime() or RotateTowardsOverTime() will cancel any pending rotations on this transform.
     /// </summary>
     public static void RotateOverTime(this Transform transform, Vector3 degrees, float seconds)
     {
         RotateOverTime[] oldRotateOverTimeComponents = transform.gameObject.GetComponents<RotateOverTime>();
         foreach (RotateOverTime oldRotateOverTimeComponent in oldRotateOverTimeComponents)
         {
             GameObject.Destroy(oldRotateOverTimeComponent);
         }
 
         RotateOverTime rotateOverTimeComponent = transform.gameObject.AddComponent<RotateOverTime>();
         rotateOverTimeComponent.hideFlags = HideFlags.HideInInspector;
         rotateOverTimeComponent.Degrees = degrees;
         rotateOverTimeComponent.Seconds = seconds;
     }
 
 class RotateOverTime : MonoBehaviour
 {
     public Vector3 Degrees { get; set; }
     public float Seconds { get; set; }
 
     private Vector3 rotationCompleted = Vector3.zero;
     private Vector3 speed;
     private Vector3 startRotation;
 
     void Start()
     {
         speed = GetBalancedRotationSpeeds(Degrees, Seconds);
         startRotation = transform.eulerAngles;
     }
 
     void FixedUpdate()
     {
         UpdateRotation();
         if (IsRotationComplete())
         {
             Destroy(this);
         }
     }
 
     private Vector3 GetBalancedRotationSpeeds(Vector3 degrees, float seconds)
     {
         if (seconds == 0)
         {
             seconds = 1;
         }
         float degreesWeight = (Degrees.x + Degrees.y + Degrees.z) / 3;
         float speedModifier = degreesWeight / seconds;
         float totalChangeInDegrees = Math.Abs(degrees.x) + Math.Abs(degrees.y) + Math.Abs(degrees.z);
         float xWeight = Math.Abs(degrees.x) / totalChangeInDegrees;
         float yWeight = Math.Abs(degrees.y) / totalChangeInDegrees;
         float zWeight = Math.Abs(degrees.z) / totalChangeInDegrees;
         float xSpeed = xWeight * speedModifier * 3;
         float ySpeed = yWeight * speedModifier * 3;
         float zSpeed = zWeight * speedModifier * 3;
         return new Vector3(xSpeed, ySpeed, zSpeed);
     }
 
     private void UpdateRotation()
     {
         rotationCompleted += Time.deltaTime * speed;
         Vector3 rotation = Quaternion.Euler(rotationCompleted + startRotation).eulerAngles;
         bool rotationIsValid = !(float.IsNaN(rotationCompleted.x) || float.IsNaN(rotationCompleted.y) || float.IsNaN(rotationCompleted.z) && float.IsNaN(startRotation.x) || float.IsNaN(startRotation.y) || float.IsNaN(startRotation.z) || float.IsNaN(rotation.x) || float.IsNaN(rotation.y) || float.IsNaN(rotation.z));
         if (!rotationIsValid)
         {
             Destroy(this);
         }
         transform.eulerAngles = rotation;
     }
 
     private bool IsRotationComplete()
     {
         bool xRotationIsComplete = Math.Abs(rotationCompleted.x) >= Math.Abs(Degrees.x);
         bool yRotationIsComplete = Math.Abs(rotationCompleted.y) >= Math.Abs(Degrees.y);
         bool zRotationIsComplete = Math.Abs(rotationCompleted.z) >= Math.Abs(Degrees.z);
         return xRotationIsComplete && yRotationIsComplete && zRotationIsComplete && Seconds != 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

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

20 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

Related Questions

Flip over an object (smooth transition) 3 Answers

how to tilt boat on turning 2 Answers

I could not be more lost (rotation script) 0 Answers

Setting rotation of game object relative to ground does not work 1 Answer

transform.Rotate will not work 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