Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by T0mC · Feb 02, 2018 at 02:33 AM · rotationgravityrotate objectmouselookrotation axis

Rotate the player around the z axis, using a mouse look type script

I've done a lot of research looking for an answer on this already, but am yet to find one. I have a game where you press a button and the player will flip gravity, all good. However when I do this I obviously want the player to rotate along with it so it looks right when walking on the ceiling. I have managed to get it to simply rotate to a fixed 180 degrees in the z axis, but to look better I want to use deltaTime in order to gradually rotate the character. Now I have been able to partially do this but the player object becomes tilted and not at 0 when the x axis changes to -180. With this, the whole rotation values changes as you rotate the camera, where the z axis and x axis keep changing between 180 and -180 degrees when the y axis changes between 90 degrees. Another thing is when rotating via 180 * Time.deltaTime, the rotation doesn't really hit 180. It's either too short or if I have the rotation continuously going it goes from 179 degrees to -179 degrees and doesn't seem to hit 180. The images show when the player is upside down, depending on the y value the values of X and Z are different, the top being the desired rotation continuously and the bottom being what I want to get rid of essentially. alt text alt text All I want is a gradual rotation along Z so the player can be upside down and everything can work the same regardless of whether you're on the roof or on the floor, ideally while still being able to look around like you normally would while you are switching gravity.

The code is as follows:

 void Update () {

         Vector2 mouseDirection = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse 
         Y"));
         mouseDirection = Vector2.Scale(mouseDirection, new Vector2(sensitivity * smoothing, sensitivity 
         * smoothing));
         smoothV.x = Mathf.Lerp(smoothV.x, mouseDirection.x, 1f / smoothing);
         smoothV.y = Mathf.Lerp(smoothV.y, mouseDirection.y, 1f / smoothing);

         mouseLook += smoothV;

     if (Input.GetKeyDown(KeyCode.G))
     {
         gravityFlipped = !gravityFlipped;
     }

     if (gravityFlipped)
     {


         if (zRotation > 179)
         {
             zRotation = 180;

         }
         else
         {
            zRotation += 180 * Time.deltaTime;
         }

         mouseLook.y = Mathf.Clamp(mouseLook.y, -70f, 70f);
         player.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, Vector3.up) * 
         Quaternion.AngleAxis(zRotation, Vector3.forward);
         transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
     }
     else
     {
         if (player.transform.localRotation.z == 0)
         {
             mouseLook.y = Mathf.Clamp(mouseLook.y, -70f, 70f);
             transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
             player.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, player.transform.up);
         }
         else
         {
             if (zRotation < 0)
             {
                 zRotation = 0;
             }
             else
             {
                 zRotation -= 180 * Time.deltaTime;
             }


             mouseLook.y = Mathf.Clamp(mouseLook.y, -70f, 70f);
             player.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, Vector3.up) * 
              Quaternion.AngleAxis(zRotation, Vector3.forward);
             transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
         }

     }


weirdissue2.png (3.0 kB)
weirdissue1.png (3.3 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

152 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 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 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 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 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 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 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 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

Rotating Two Axes for Gravity,Rotating only two axes towards Object 0 Answers

Rotating on other axis 1 Answer

,rotate object to match another object on its local 3 axis 1 Answer

Rotate an Object while moving forward using empty gameObject 0 Answers

My rotation is not the same in inspector as it is in the scirpt 0 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