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
1
Question by chas11man · Feb 27, 2013 at 12:43 AM · rotationanglekeypress

How do I rotate an object at a constant rate a specific angle on key down?

I want to rotate an object 180 degrees (turn around) at a constant rate when either the 'A' or 'D' keys are pressed down to turn left or right respectively. This is the code that I want it to work with:

 public float rotationSpeed = 50;
 void Update() {
     if (Input.GetKeyDown(KeyCode.A)) {
         // Turn left 180 degrees at 50 degrees per second
     }
     if (Input.GetKeyDown(KeyCode.D)) {
         // Turn left 180 degrees at 50 degrees per second
     }
 }

How would I go about implementing this?

Comment
Add comment · Show 2
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 robertbu · Feb 27, 2013 at 01:50 AM 0
Share

With these kinds of problems, the limiting is usually the hard work. Does it have to be limited to 180 degrees? What axis are you rotating around? Are there other rotations on this object or do you start with the object axis aligned?

avatar image chas11man · Feb 27, 2013 at 01:55 AM 0
Share

The game is a first person platformer. The object being rotated is the character that you are playing as (although it is just a box right now with a camera). The character is moving in what can effectivly be called a 2D space so the only movements are forwards, backwards, turn around, and jump. In short, yes it does have to be 180. The axis I want to rotate around is the y axis. There are no other rotations. It should only ever face 0 degrees rotation or 180 degrees on the y axis. Everything else should stay at 0.

3 Replies

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

Answer by robertbu · Feb 27, 2013 at 02:26 AM

Here is some rotation code. Note that RotateTowards() takes the shortest angle. When it is exactly 180 degrees, the direction is arbitrary. You can set the direction of rotation by an angle that is every so slightly more/less than 180 for your rotation. But it's going to turn forward and then turn back on the same path, it's not going to turn all the way around around. A few lines of code can change that.

@MickM approach will also work (and may be a bit more flexible) but I think there is a bug in his logic. It will quit rotating when the angle is >= 180, but nothing gets it back in bounds. Easy fix.

 using UnityEngine;
 using System.Collections;
 
 public class Turn180 : MonoBehaviour {
     
     public float speed = 50.0f;
     private Quaternion qTo = Quaternion.identity;
     private Quaternion qForward = Quaternion.identity;
     private Quaternion qBack = Quaternion.Euler (0.0f, 180.0f, 0.0f);
 
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown(KeyCode.A))
             qTo = qBack;
         else if (Input.GetKeyDown (KeyCode.D))
             qTo = qForward;
         
         transform.rotation = Quaternion.RotateTowards (transform.rotation, qTo, Time.deltaTime * speed);
     }
 }
Comment
Add comment · Show 4 · 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 chas11man · Feb 27, 2013 at 03:18 AM 0
Share

So this works great, however I was hoping that the 'A' key would only turn 180 to the left no matter what direction you were facing and same for 'D' although to the right. Do you think that would be hard to implement?

avatar image robertbu · Feb 27, 2013 at 05:24 AM 0
Share

I added logic so that it rotates as you specified. Note the added complexity of this code. This added complexity (in my $$anonymous$$d) is a good indication that @$$anonymous$$ick$$anonymous$$ 's solution would have been the better approach.

 public class Turn180 : $$anonymous$$onoBehaviour {
     
     public float speed = 50.0f;
     private Quaternion qTo = Quaternion.identity;
     private Quaternion qForward = Quaternion.identity;
     private Quaternion qBack = Quaternion.Euler (0.0f, 180.0f, 0.0f);
     private const float fLittleBit = 0.01f;
     private bool bFirst = true;
 
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D)) {
               if (Vector3.Angle(transform.forward, Vector3.forward) < fLittleBit) {
                 transform.rotation = Quaternion.Euler(0.0f, 0.0f+fLittleBit, 0.0f);
                 qTo = qBack;
                 bFirst = true;
             }
             else if (Vector3.Angle(transform.forward, Vector3.back) < fLittleBit) {
                 transform.rotation = Quaternion.Euler(0.0f, 180.0f+fLittleBit,0.0f);
                 qTo = qForward;
                 bFirst = false;
             }
             else {
                 if (bFirst)
                     qTo = qBack;
                 else 
                     qTo = qForward;
             }
         }
         else if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.A)) {
               if (Vector3.Angle(transform.forward, Vector3.forward) < fLittleBit) {
                 transform.rotation = Quaternion.Euler(0.0f, 0.0f-fLittleBit, 0.0f);
                 qTo = qBack;
                 bFirst = false;
             }
             else if (Vector3.Angle(transform.forward, Vector3.back) < fLittleBit) {
                 transform.rotation = Quaternion.Euler(0.0f, 180.0f-fLittleBit,0.0f);
                 qTo = qForward;
                 bFirst = true;
             }
             else {
                 if (bFirst)
                     qTo = qForward;
                 else 
                     qTo = qBack;
             }
         }
     
         transform.rotation = Quaternion.RotateTowards (transform.rotation, qTo, Time.deltaTime * speed);
     }
 }
avatar image chas11man · Feb 27, 2013 at 06:14 AM 0
Share

This worked perfectly! Thank you for the help. I also added a few lines to anchor the object to the x axis so when moving while turning you wouldn't move off center.

avatar image MickM · Feb 27, 2013 at 06:32 AM 0
Share

Yea definitely a big bug there with getting stuck at the 180!

Easy fix for that is ins$$anonymous$$d of

 if ((rotationTracker > -180)&&(rotationTracker < 180))

Use

  if (( (rotationTracker + Time.deltaTime*rotation$$anonymous$$odifier) > -180) && ( (rotationTracker + Time.deltaTime*rotation$$anonymous$$odifier) < 180) )

Will just prevent rotation if it will go outside bounds of 180 degrees. This will not ever (normally) get to 180 degrees fully but will get quite close (around the 179 degree mark)

Really depends what you want... that way is just quick and simple. (Haha definitely dont use my initial answer though - it WILL get you stuck!!!)

avatar image
1

Answer by MickM · Feb 27, 2013 at 01:59 AM

If you want it to stop at 180... Quick and dirty idea for you: Note - this is done quickly on iPad so you will have to actually put the below into code and test BUT it should do what you need!

 rotationTracker = 0
 rotationModifier = 1
 
 Function update()
 If keycode.a
    rotationModifier =1
 
 If keycode.d
     rotationModifier =-1
 
 if (rotationTracker>-180)&&(rotationTracker<180)
     rotationTracker += Time.deltaTime x rotationModifier
     transform.Rotate(0, rotationModiferx50xTime.deltaTime, 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 sjurick · Aug 20, 2013 at 11:36 PM

I have an object that I want to rotate left, right, forward and backward using the arrow keys. It works, but my beta testers are saying the motion is too linear and want to be able to control the amount of rotation more.

I thought of doing something like adding exponential interpolation or Quaternion, but don't know how to implement it.

What I want to happen is have it to where when an arrow key is pressed, the object starts rotating in that direction, but slowly at first, then exponentially getting faster the longer its held down.

My code so far:

public class Tilt : MonoBehaviour {

 public int tiltSpeed = 1;
 
 void FixedUpdate () {
 
 if(Input.GetButton("Tilt Forwards")) {
         transform.Rotate(tiltSpeed * 1,0,0);
     Debug.Log("Up Arrow Key Pressed!");
     }
     
 if(Input.GetButton("Tilt Backwards")) {
         transform.Rotate(tiltSpeed * -1,0,0);
     Debug.Log("Down Arrow Key Pressed!");
     }
     
 if(Input.GetButton("Tilt Left")) {
         transform.Rotate(0,0,tiltSpeed);
     Debug.Log("Left Arrow Key Pressed!");
     }
     
 if(Input.GetButton("Tilt Right")) {
         transform.Rotate(0,0,tiltSpeed * -1);
         Debug.Log("Right Arrow Key Pressed!");
     }
 }

}

Comment
Add comment · Show 1 · 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 robertbu · Aug 20, 2013 at 11:37 PM 0
Share

sjurick - this needs to be opened as a new question, not posted as an 'answer' to an existing question. I'll be deleting it shortly.

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

11 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

Related Questions

Problems caused by non-unique Euler angle solutions 1 Answer

Restricting rotation to multiples of 15? 1 Answer

How do I apply rotations to parent child using Rigidbody? 0 Answers

90 Degree stopping rotation on y-axis issue 0 Answers

How can I fix this randomly rotating gun to an unknown degrees? 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