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 wiz_rares · Aug 03, 2014 at 10:46 PM · quaternionrotatespeed

How to rotate object by X degrees with Y speed C#

Hi. I've been trying to get this to work for a few days now and couldn't find an exact answer for what I needed, after scouring Google and the Unity forums.

I'm trying to rotate an object by a certain amount of degrees, with a fixed speed. Looking on the forums I've found how to rotate an object just using time, but I need a rotation speed, not a time in which to rotate the object. The reason why I need this is because I'm setting the target rotation with a gesture (swiping) and then I need the object to follow rotating at a certain speed. Here's the code:

     void Update () 
     {
         if (shouldRotate == true) 
         {
             Rotate2 (rotateBy);
         }
     }
 
     public void Rotate2(float rotationAngle)
     {
         Quaternion oldRotation = transform.rotation;
         transform.Rotate(0,0,rotationAngle);
         Quaternion newRotation = transform.rotation;
 
         transform.rotation = Quaternion.Slerp(oldRotation, newRotation, Time.time * rotateSpeed);
 
         shouldRotate = false;
     }

At the moment I'm calling shouldRotate = true by pushing E from the controller class. The problem is that the object moves the amount of degrees IMMEDIATELY, not slowly. I've tried modifying the speed but the rotation is INSTANT, which leads me to believe there's a problem with my code. Let me know what you think.

Wiz

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
1

Answer by robertbu · Aug 03, 2014 at 10:58 PM

There are a several things wrong in this code, and without understand what you are trying to do, I cannot figure out how to redo this code so it gives you what you want. If 'rotationAngle' represents degrees per second, you can do:

 transform.Rotate(0,0,rotationAngle * time.deltaTime);

Another way is to use Quaternion.RotateTowards(). If you execute something like the following each frame from Update():

  transform.rotation = Quaternion.RotateTowards(transform.rotation, newRotation, Time.deltaTime * rotateSpeed); 

But you cannot mixing and matching the two as you've done here both has problems, and any working result would be UGLY. Note the use of 'Time.deltaTime', not 'Time.time'.

In general if you know the destination of your rotation, use RotateTowards() and set the destination once (not repeatedly each frame). If you just want the object to spin, use Transform.Rotate().

Comment
Add comment · Show 2 · 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 wiz_rares · Aug 04, 2014 at 07:29 PM 0
Share

Thank you for answering. I basically have an object that I want to rotate by X angles with a certain speed, NOT in a certain amount of time.

I did not know what was the best way to do it, I looked around the internet and the result is a mix of what I found as I applied it to my problem. If you have another suggestion, do let me know.

avatar image robertbu · Aug 04, 2014 at 11:57 PM 0
Share

I want to rotate by X angles with a certain speed

You can never know too much before picking the best rotation solution. For example here are other factors:

  • Does the code need to handle rotations greater than 180 degrees

  • Are all the rotations to be axes aligned with either the local axes or the global axes?

  • Does you need the ability to change or interrupt the rotation while it is still rotating?

Here is a script with two different versions of RotateBy(). The first will handle rotations where the amount rotated by is greater than 180 degrees. The second is a bit simpler and always takes the shortest path between the start and end rotations. Neither are currently changeable or interrupt-able. You should be able to drop either coroutine into your class and call them with:

 RotateBy(rotateBy, transform.forward, rotateSpeed);

In a new scene, put the script below on an object and hit space while it is running.

 using UnityEngine;
 using System.Collections;

 public class Example : $$anonymous$$onoBehaviour {

 bool rotating = false;
 
 void Update() {

     if (!rotating && Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) {
         Vector3 axis = Random.rotation * Vector3.up;
         float angle = Random.Range (-125.0f, 125.0f);
         float speed = Random.Range (45.0f, 135.0f);

         Debug.Log ("Rotating by " + angle + " degrees on axis " + axis + " at " + speed + " per second.");
         StartCoroutine(RotateBy(angle, axis, speed));
     }
 }

 IEnumerator RotateBy(float angle, Vector3 axis, float speed) {
     rotating = true;
     Quaternion start = transform.rotation;

     float curAngle = 0.0f;

     while ($$anonymous$$athf.Abs (curAngle - angle) > 0.0001f) {
         curAngle = $$anonymous$$athf.$$anonymous$$oveTowards (curAngle, angle, Time.deltaTime * speed);
         transform.rotation = Quaternion.AngleAxis (curAngle, axis) * start;
         yield return null;
     }

     transform.rotation = Quaternion.AngleAxis (angle, axis) * start;
     rotating = false;
 }

 IEnumerator RotateBy2(float angle, Vector3 axis, float speed) {
     rotating = true;
     Quaternion dest = Quaternion.AngleAxis (angle, axis) * transform.rotation;

     while (transform.rotation != dest) {
         transform.rotation = Quaternion.RotateTowards (transform.rotation, dest, Time.deltaTime * speed);
         yield return null;
     }

     rotating = false;
 }
 }

 

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

2 People are following this question.

avatar image avatar image

Related Questions

How rotate only z axis of Gameobject (2D) 1 Answer

Incrementing Rotation Produces Strange Transform Values 1 Answer

Cannot instantiate rotation on Prefab 1 Answer

Rotation with different speed for different axis 2 Answers

rotate an object on the z axis so that it does not exceed 0-45 degrees 0 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