• 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 madmike6537 · Jan 30, 2013 at 02:22 AM · transform.rotate

Trying to rotate character 180 degrees slowly - having issues

Hi Unity,

I am trying to make my character model rotate at a set speed for 180 degrees, while still moving forward the same direction ( because he moves forward at all times) but running into some issues. I have looked through a few scripts on here and this is what I have come up with:

(There is some code in here that also checks if you "Double Tapped" the A button instead of just pressing it.)

             if(Input.GetKeyDown(KeyCode.A))
             {
                 if(!oneTap)  //check to see if this is the first time we pressed A
                 {
                     oneTap = true;
                     timer_for_double_tap = Time.time;  //save the current time
                     
                     //Add any functionality for just pressing "A" here... if you wanted
                 }
                 else
                 {
                     Debug.Log("Double Tap");
                     oneTap = false;  // found a double tap
                     Transform boarderModel = GameObject.Find("SpaceBoarderModel").transform;
                     
                     //Do a 180 rotation Trick!
 
                    if (!rotating) 
                     {
                 
                       rotating = true;
                 
                       float curRot = 0;
                 
                       float startRot = boarderModel.eulerAngles.y;
                 
                       while (curRot < 180) {
                 
                          curRot += rotateSpeed * Time.deltaTime;
                 
                          startRot = startRot + curRot;
                 
                          break;
                 
                       }
                 
                       startRot = Mathf.Round(startRot + 180);
                 
                       rotating = false;
                 
                    }
                 }

First I check to see if the user has tapped "A" twice, if they have, I want to start the rotation and rotate by "rotateSpeed" to 180 degrees. I use boarder model to get the transform of the actual model of my character, which is housed inside an empty gameObject named something else. The idea behind this was that the parent gameobject would continue to move forward while the child that I reference here will do the 180.

I hope that makes sense.

There is no errors, but my character does not receive any updates to their transform and of course does not rotate. Any ideas what I might have done wrong?

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

Answer by robertbu · Jan 30, 2013 at 04:54 AM

In Unity you accomplish a little bit of motion every frame. The primary place to make this change is in Update() or some method called from inside Update(). You first problem is that you have a while loop that does the rotation all at once rather than a little bit at a time. You want a rotation of rotateSpeed * Time.deltaTime at every call to Update.

Your second problem I think (since I'm not seeing all your code), is that you are not doing anything to make the object rotate. That is there are many ways to rotate an object in Unity. For example, you can call transform.Rotate() to incrementally rotate the object, or you can directly assign a specific rotation to transform.eulerAngles.

Here is a simple script that will rotate the object it is attached to around the Y axis by the number of degrees per second specified.

 var degreesPerSecond = 30.0;
 
 function Update () {
     transform.Rotate (Vector3.up * degreesPerSecond * Time.deltaTime);
 }
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 gfvfubb · Jan 30, 2013 at 09:17 AM 0
Share

There are more rotate functions also. You can set transform.rotation = Quaternion. And generate quaternion interpolations with Quaternion.Lerp, .Slerp, and .LerpAngle. Check the wiki for controllers for examples, or google Unity WoW camera. You can also set .angularVelocity on rigidbody.

avatar image madmike6537 · Feb 01, 2013 at 04:56 AM 0
Share

Thanks guys - got it working with your help.

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

10 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

Related Questions

Issues with copying Y axisrotation of another object 2 Answers

Turn page, smooth rotate 1 Answer

unity how to make objects look at random objects with the same tag 1 Answer

How to stop a child from orbiting around parent? 0 Answers

Character controller Rotate around the object 2 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