• 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 W1k3 · Jul 27, 2013 at 09:50 PM · rotation

How can I rotate an object 180 degrees? (C#)

I'm unsure what code to use for this. I want to be able to rotate an object 180 degrees on the Y axes smoothly.

I don't know if I should be using transform.eulerAngles or Quaternion.Lerp. I don't even know the difference.

I don't need anything too complicated, it just has to be able to make the rotation.

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

3 Replies

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

Answer by Forsyth · Jul 28, 2013 at 01:02 AM

You can use Vector3.Lerp to transition from your current euler angles to a set of target angles 180 degrees around the y axis (Vector3.up).

 public float smooth = 1f;
 
 private Vector3 targetAngles;
 
 void Update () 
 {
     if(Input.GetKeyDown(KeyCode.S)) // some condition to rotate 180
         targetAngles = transform.eulerAngles + 180f * Vector3.up; // what the new angles should be
     
     transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, targetAngles, smooth * Time.deltaTime); // lerp to new angles
 }

Just so you know, transform.eulerAngles is essentially the angles X, Y, and Z that appear under transform in the inspector.

You can also use Quaternions to do the same thing. It's neater in code but slightly more performance costly. You can set the target rotation to the opposite direction you're looking.

 public float smooth = 1f;
 
 private Quaternion targetRotation;
 
 void Update () 
 {
     if(Input.GetKeyDown(KeyCode.S)) // some condition
         targetRotation = Quaternion.LookRotation(-transform.forward, Vector3.up);
     
     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, smooth * Time.deltaTime);
 }

Hope this helps.

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 jeroen13m · Mar 20, 2017 at 01:13 PM 0
Share

Does this also work for rotating around the X axis? if not, could you help me?

avatar image
6

Answer by RyanZimmerman87 · Jul 27, 2013 at 10:36 PM

Rotation Y axis instantly:

transform.RotateAround (transform.position, transform.up, 180f);

Rotation Y axis over time:

transform.Rotate(0, Time.deltaTime * 30, 0, Space.Self);

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 W1k3 · Jul 28, 2013 at 12:21 AM 0
Share

I'm looking for a bit of both. I need it to rotate over time, but I need it to stop once it has rotated 180 degrees. Thanks though, this explains allot already.

avatar image
2

Answer by Ballowii · Mar 15, 2016 at 08:51 PM

 public float hight=5;
         private float spincount;
         private float jumpCount;
         private float degree;
         private float spinspeed;
         void Start () {
             jumpCount = 0;
             spincount=1;
             degree = 360;
             spinspeed = 6;
         }
         void Update() {
             //Jumping
             if(Input.GetKeyDown (KeyCode.Space)) {
                 rigi.velocity = new Vector3 (0, hight, 0);
                 jumpCount = 1;
             }
                //Rotating
             if (jumpCount == 1 && spincount < degree/spinspeed) {
                 transform.Rotate (Vector3.back*spinspeed);
                 spincount += 1;
             } else {
                 jumpCount = 0;
                 spincount = 0;
             }
         }
     }

Here you go, my whole script for a jumping cube rotating an exact degree smoothly. Just modifiy it for your needs. It tried everything else too, but this was the only one which really works reliably even while jumping.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Rotating a character with the joystick of the controller 1 Answer

Rotating Player relative to Camera Rotation 2 Answers

Need help in setting a game-object's rotation to euler-degrees. 0 Answers

Rotating Child Around World Space 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