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 Blink · May 26, 2015 at 06:44 PM · objectrotatetimeaxis

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

Hello, Ive been trying to write a script which rotates an object 90 degrees on the Y axis after an amount of time has passed but I cant figure out what it is I have to do. This is my attempt but im far off what Im trying to achieve, can someone help me out please?

  public var speed = 55.0;
  private var rotation = 0.0;
  private var qTo = Quaternion.identity;
  
  
  function Update () {
   
      if (Input.GetButtonDown("f")) {
          rotation += 90.0;
          qTo = Quaternion.Euler(0.0, 0.0, rotation);
          
      }
       
      
      transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, speed * Time.deltaTime);
      
  }

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

Answer by Eno-Khaon · May 26, 2015 at 07:00 PM

I'm going to take your "90 degrees every 5 minutes" literally here:

 // Javascript
 
 public var fiveMinutes: float = (60.0 * 5.0); // 300 seconds
 public var degreesPerTime: float = 90.0; // 90 degrees per 5 minutes
 public var rotationAxis: Vector3 = Vector3.up; // Y-axis
 
 function Update()
 {
     transform.rotation *= Quaternion.AngleAxis((degreesPerTime / fiveMinutes) * Time.deltaTime, rotationAxis);
 }

If you're looking for a quick rotation to occur after every 5-minute interval has elapsed, you'll want a rolling timer to compare against Time.time and you'll want to use Quaternion.Lerp() to guarantee accurate stopping points for the rotations at a time.

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 Blink · May 27, 2015 at 01:27 AM 0
Share

Thanks, yeah I worded my question badly Im trying to make an object rotate 90 degrees at 5 $$anonymous$$ute intervals. Ive wrote this script however I do not fully understand how to use Quaternion.Lerp() since it needs a "from" and "to" transform where I dont know what to write. How do I alter this to make it work correctly?

 var speed = 0.1;
 InvokeRepeating("Rotate", 0, 2);
 
 function Rotate () {
 
 transform.rotation = Quaternion.Lerp(????, Time.time * speed);
 }
avatar image Eno-Khaon · May 27, 2015 at 03:41 AM 0
Share

The rotation you would want is your initial rotation when the spin is going to start, then multiply that by the AngleAxis rotation when for your ending rotation:

 // Javascript
 
 public var five$$anonymous$$inutes: float = (60.0 * 5.0); // 300 seconds
 public var degreesPerRotation: float = 90.0; // 90 degree rotations every 5 $$anonymous$$utes
 public var rotationAxis: Vector3 = vector3.up; // Y-axis
 
 public var timeToSpin: float = 1.0; // 1 second spent spinning 90 degrees
 
 private var rotateTimeStamp: float = 0.0; // $$anonymous$$ark the start of a spin
 private var rotationStart: Quaternion; // Start of rotation
 private var rotationEnd: Quaternion; // End of Rotation
 
 function Start()
 {
     StartCoroutine(DelayedSpin);
 }
 
 function DelayedSpin()
 {
     while(true) // Another way to just keep going. (only applicable when used in conjunction with "yield" statements)
     {
         yield WaitForSeconds(five$$anonymous$$inutes - timeToSpin); // Total time $$anonymous$$us time spent, so the whole cycle starts every five $$anonymous$$utes
     
         rotationStart = transform.rotation;
         rotationEnd = transform.rotation * Quaternion.AngleAxis(degreesPerRotation, rotationAxis); // QuaternionA * QuaternionB is a concatenation of rotations.
         rotateTimeStamp = Time.time;
     
         while(Time.time - rotateTimeStamp < timeToSpin)
         {
             transform.rotation = Quaternion.Lerp(rotationStart, rotationEnd, (Time.time - rotateTimeStamp) / timeToSpin); // or Slerp for a smoother acceleration in and out of the rotation
             yield; // Wait for next frame
         }
         transform.rotation = rotationEnd; // $$anonymous$$ake sure it ends on cue
     }
 }

This is untested, but should be more what you were looking for now, I think. ;)

avatar image Blink · May 27, 2015 at 11:05 AM 0
Share

Cheers for this, got this bad boy very nearly perfect however how do I change the speed at which the object spins? At the moment increasing the "timeToSpin" variable makes the rotation occur faster and decreasing it makes it just snap to the different rotation. Any ideas? I also tweaked the script a wee bit..

  public var spinInterval: float = 60;
  public var degreesPerRotation: float = 90.0; 
  public var rotationAxis : Vector3 = Vector3.up; 
  
  public var timeToSpin: float = 1.0; // 1 second spent spinning 90 degrees
  
  private var rotateTimeStamp: float = 0.0;
  private var rotationStart: Quaternion; 
  private var rotationEnd: Quaternion;
  
  function Start()
  {
    
      DelayedSpin();
  }
  
  
  function DelayedSpin()
  {
      while(true)
      {
          yield WaitForSeconds(spinInterval - timeToSpin); 
      
          rotationStart = transform.rotation;
          rotationEnd = transform.rotation * Quaternion.AngleAxis(degreesPerRotation, rotationAxis); 
          rotateTimeStamp = Time.time;
      
          while(Time.time - rotateTimeStamp < timeToSpin)
          {
              transform.rotation = Quaternion.Lerp(rotationStart, rotationEnd, (Time.time - rotateTimeStamp) * timeToSpin); 
              yield; 
          }
          transform.rotation = rotationEnd;
      }
  }
avatar image Eno-Khaon · May 27, 2015 at 06:47 PM 0
Share

Oh, whoops! $$anonymous$$y mistake there. It was supposed to be a division by timeToSpin rather than multiplication. I've updated my previous comment to include this line ins$$anonymous$$d:

 transform.rotation = Quaternion.Lerp(rotationStart, rotationEnd, (Time.time - rotateTimeStamp) / timeToSpin);

(Time.time - rotateTimeStamp) gives a single-second rotation time baseline. Divide that by the intended duration to get it to last longer. Sorry about that, I just got that one mixed up.

avatar image Blink · Jun 02, 2015 at 04:00 PM 0
Share

Thats got it, thanks for your help Eno :)

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

Rotate Object to face 2 axis? 1 Answer

How do we write a C# script to smoothly rotate objects based on Input.GetAxis("Mouse *")? 1 Answer

Add force at position Question 1 Answer

Rotate rigidbody using MoveRotation() around the global y axis 2 Answers

Make a cube rotate in right or left direction randomly 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