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 Shapes · Feb 16, 2014 at 07:52 AM · rotationbuttonspeedcommands

Slowing Down Rotation Speed

I am trying to get a cube to rotate on button command. I have the button command code down, but the rotation is happening to quick. How do I slow down the rotation speed?

Here is one of the code for the button command rotation( I separated the left and right control from the up and down code)

 function Start () 
 {
 
 }
 
 function Update() {
 if (Input.GetKey ("left")) {
 transform.Rotate(0, 90, 0);
 }
 if (Input.GetKey ("right")) {
 transform.Rotate(0, -90, 0);
 }
 }
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 Scribe · Feb 16, 2014 at 03:56 PM

You can slow it down very simply by giving it a value smaller than 90 such as:

 transform.Rotate(0, 1, 0);

which would rotate the object at 1 degree per frame. If you have a set 'degrees per second' that you want to rotate by you can use Time.deltaTime to achieve that effect:

 transform.Rotate(0, 10*Time.deltaTime, 0);

will rotate at 10 degrees per second.

Scribe

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 Shapes · Feb 16, 2014 at 10:06 PM 0
Share

Thank you for the suggestions, but I am trying to rotate the cube 90 degrees every time i press a button.

avatar image Scribe · Feb 17, 2014 at 02:30 AM 0
Share

Then you transform.Rotate is probably not the best method to use. Rotate is designed for continuous rotations, not so much for where you want to rotate a fixed amount. You can still do it with rotate but it becomes more difficult. I answered a similar question to this recently maybe you can make use of the code:

 private var startAngle : float;
 private var targetAngle : float;
 private var startTime : float;
 var smooth : float = 1.0; // The number of seconds taken to rotate
 private var angle : float;
 
 function Start(){
     angle = transform.eulerAngles.y;
 }
 
 function Update () {
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)){
        startTime = Time.time;
        startAngle = angle;
        targetAngle = targetAngle + 90;
     }else if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)){
        startTime = Time.time;
        startAngle = angle;
        targetAngle = targetAngle - 90;
     }
  
     angle = $$anonymous$$athf.LerpAngle(startAngle, targetAngle, (Time.time - startTime)/smooth);
     transform.eulerAngles = Vector3(0, angle, 0);
 }

This sets the transform.eulerAngles directly rather than using Rotate.

Scribe

avatar image Shapes · Feb 18, 2014 at 04:43 AM 0
Share

Thank you so much for the script, it was exactly what i was looking for. The only problem I have found with the script is that when i play the game i can rotate the cube left (with left arrow) and right(with the right arrow) but not up and down, I have change the script so that the cube can move up and down but not left or right. Is there any way that i can rotate the cube left, right, up, and down?

avatar image Scribe · Feb 19, 2014 at 01:23 AM 0
Share

When you are rotating in two axis simultaneously it becomes quite a bit harder to calculate the rotation as you don't have a fixed axis to pivot around so I don't think you can extend the previous method to work in 2 axis (though maybe someone can correct me).

I feel like there should be an easier way to do this but I can't think of one though I may investigate using the cross product of the two axis and rotating about that.

Anyway, in the meantime this produces a very smooth rotation that will always finished locked to a multiple of 90degrees.

 var targetAngle : Quaternion;
 var startAngle : Quaternion;
 
 var forward : Vector3;
 var right : Vector3;
 var up : Vector3;
 
 private var startTime : float;
 
 function RoundVector(v : Vector3) : Vector3{
     return Vector3($$anonymous$$athf.Round(v.x), $$anonymous$$athf.Round(v.y), $$anonymous$$athf.Round(v.z));
 }
 
 function RotateAround(vF : Vector3, vR : Vector3, vU : Vector3, axis: Vector3, angle: float) : Quaternion{
     var rot: Quaternion = Quaternion.AngleAxis(angle, axis); // get the desired rotation
     forward = RoundVector(rot*vF);
     right = RoundVector(rot*vR);
     up = RoundVector(rot*vU);
     
     startTime = Time.time;
     startAngle = transform.rotation;
     targetAngle = rot * targetAngle;
 }
 
 function Start(){
     startAngle = transform.rotation;
     targetAngle = startAngle;
     forward = RoundVector(transform.forward);
     right = RoundVector(transform.right);
     up = RoundVector(transform.up);
 }
 
 function Update () {
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)){
         RotateAround(forward, right, up, Vector3.up, -90);
     }else if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)){
         RotateAround(forward, right, up, Vector3.up, 90);
     }
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow)){
         RotateAround(forward, right, up, Vector3.right, 90);
     }else if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.DownArrow)){
         RotateAround(forward, right, up, Vector3.right, -90);
     }
     
     transform.rotation = Quaternion.Slerp(startAngle, targetAngle, Time.time-startTime);
 }

Scribe

avatar image Shapes · Feb 24, 2014 at 05:20 AM 0
Share

Thank you so much the script works great and it was exactly what i was looking for.

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

19 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

Related Questions

Multiple Cars not working 1 Answer

Object Rotation/Character Speed 1 Answer

How Do I Add speed to my rotation 1 Answer

When I rotate my player in open 3 space, the transform rotation component keeps snapping back to origin. However, when I disable my rigidbody.rotate command, the transform rotation works just fine. 0 Answers

Array weapons Wheel scroll switch 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