• 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 greatUnityGamer · Sep 15, 2013 at 03:24 AM · transformrotatefacing

Is there a Method to Set the Exact rotation instead of ADDING rotation?

transform.rotate(Vector3)

rotates but it rotates. meaning it adds to the current facing direction.

But what if i want to RESTORE my initial facing direction of a character in some part of the game?

Like my character is facing right at the start of the game and then pressing both W and D at the same time makes him run diagonal forward to the right instead of just to the right.

and i want that after the user lets go of those 2 keys, then the character should face the right side again.

Problem is, if i do transform.rotate( originalangle); it's not gonna be the original facing direction. it's gonna add the original angle to the current and so it's not gonna end up looking facing right.

Here's what i have so far and it didn't work

 float myOriginalRotationY;
 void Start(){
   myOriginalRotationY = transform.eulerAngles.y;
 }
 
 void Update () {
         
     //check combination keys 
     //4 combinations
     //W+ D, ,   W + A, ,   S + A, ,  S + D
         //Here in this example I'm just starting with testing
         //W+D combo
     if( Input.GetKey(KeyCode.W) &&      Input.GetKey(KeyCode.D)) {    //forward diagonal right
 
           
     if(currentDir !=7){
 
           //Rotate Based on current facing direction
           //-2 is when user pressed the S to face down
           //or to run down.  1 is D for facing right
           //-1 is A for facing left
       if(currentDir == -2)    
        transform.Rotate ( 0, 135, 0, Space.Self);
           if(currentDir == -1)    
            transform.Rotate ( 0, -135, 0, Space.Self);
       if(currentDir == 1)    
        transform.Rotate ( 0, -45, 0, Space.Self);
                 
            currentDir = 7;
     }
     return;
 }
         
         
 //once done with the Press & Hold the above, return to //either look left or right
 //depending on the keycombination executed
 
 if( currentDir == 7){                                        //*******FORWARD DIAGONAL RIGHT
       if( Input.GetKeyUp(KeyCode.W) && Input.GetKeyUp(KeyCode.D)){    
       
     Debug.Log( "user let go the Diagonal");
     //Return to original Right direction
 
        transform.Rotate ( transform.eulerAngles.x, myOriginalRotationY, transform.eulerAngles.z);
                 
     currentDir = 1;
        }
        return;
 }    
 
 //Then there's more stuf in the update
 //handdling if single keys W A D S were pressed and changing //chracter rotation for each




So anyways, my way of trying to return my character to the original facing direction didn't work. How do i make it work?

By the way, the game i'm making, is 3D but not FPS. Basically you have a camera at about the same level of the character but at a fixed distance from the character. The player can use the W S D A keys to move left or right or forward or towards the camera.

Then press key combinations liek W+ D to walk in the diagonal forward right, diagonal forward left, and so on.

Anyways, this question is about HOW TO RESTORE ORIGINAL FACING DIRECTION(either left or right). How to store the original facing direction and restore it after my walking diagonal move.

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

2 Replies

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

Answer by aldonaletto · Sep 15, 2013 at 03:30 AM

Save the initial Euler angles and restore it when needed;

 Vector3 myOriginalRotation;
 
 void Start(){
   myOriginalRotation = transform.eulerAngles;
 }
  
 void Update () {
        ...
        // restore original rotation:
        transform.eulerAngles = myOriginalRotation;
        ... 
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 greatUnityGamer · Sep 15, 2013 at 05:09 AM 1
Share

thanks awesome. i finally got it

avatar image clunk47 · Oct 09, 2013 at 06:15 PM 1
Share

@greatUnityGamer: If you "got it", Accept the answer that helped you get it. Vote it up as well.

avatar image
2

Answer by vexe · Sep 15, 2013 at 03:35 AM

Wanna restore everything back the way it was? - Just store your initial rotation and then fallback to it.

 Quaternion initRotation = transform.rotation;

And then when you want to set it back:

 transform.rotation = initRotation;

You asked about a way to set instead of add rotation, here it goes transform.rotation - it gives you back a Quaternion, so you could do stuff like:

 transform.rotation = Quaternion.Euler(0, 90, 0);

This is equivalent to setting the rotation to (0, 90, 0) in the inspector.

Hope that helped.

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 greatUnityGamer · Sep 15, 2013 at 05:09 AM 0
Share

Thanks alot that was a great answer

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

18 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

Related Questions

transform RotateAround 1 Answer

smooth fps movements using joystick 1 Answer

Mesh does not rotate around custom Pivot 1 Answer

unknown axis of rotation 1 Answer

Getting an object to rotate back to zero after key is released 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