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
0
Question by triangle4studios · Jan 22, 2021 at 01:04 AM · c#rotationquaternionplayer movementnormal

Align Player to Regional Up Direction, Without Altering Look Direction Dramatically

Alright, this is a complicated one, and I straight up apologize for any nose bleeds that result because of it.


I have a player can move in 360 degrees of rotation. I have platforms that have their own unique gravity, that is, the player is pulled to the ground when he enters the Spherical radius of one of these platforms.

I am not, and for obvious physics reasons cannot, use unity's built in gravity, but my faux gravity works fine.

What I cannot do, is align my player to the local gravity.


The MOST IMPORTANT THING HERE IS, when he is inside the gravitational sphere, he can only rotate left and right and up and down. No roll.


So the only thing I need to accomplish, is I need him to align his body to the ground without rotating to the direction of the normal.

I hope this is making sense.

I need the player to face up(no problem accomplishing that using hit.normal), and then I need him to remain facing in the general direction he was facing.


Using hit.normal always sets the transform.up to the normal up, and transform.forward to the normals forward. Which is always the same direction.


In laymens terms, if he is facing more east than any other direction when he enters the gravity field, I want him to face east and up. Not just up and whatever direction the hit normal is.

 if(!gravity){
             if(Input.GetKey(KeyManager.code.rollLeft)){roll += rollStep;}
             else if(Input.GetKey(KeyManager.code.rollRight)){ roll += -rollStep;}
             else roll = 0;
             body.transform.Rotate(Vector3.up, Input.GetAxis("Mouse X") * lookSensitivity, Space.Self);
             body.transform.Rotate(Vector3.right,-Input.GetAxis("Mouse Y") * lookSensitivity, Space.Self); 
             body.transform.Rotate(Vector3.forward, roll, Space.Self);
             justSwitched = true;
              
         }else{
             // THIS IS THE PROBLEM AREA BELOW HERE.
             if(justSwitched){
                 
                 if(Physics.Raycast(body.transform.position, -gravitySource.up, out hit))
                 {
                     body.transform.up = hit.normal;
                     head.transform.up = hit.normal;
                 }
                 justSwitched = false;
             }
             float h = lookSensitivity * Input.GetAxis("Mouse X");
             float v = lookSensitivity * Input.GetAxis("Mouse Y");
 
             Vector3 bodyAxis = new Vector3(0, h, 0);
             Vector3 headAxis = new Vector3(-v, 0, 0);
             
             head.transform.Rotate(Vector3.Lerp(transform.rotation.eulerAngles, headAxis, 1), Space.Self);
             body.transform.Rotate(Vector3.Lerp(transform.rotation.eulerAngles, bodyAxis, 1), Space.Self);
         }


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

156 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 avatar image avatar image avatar image avatar image

Related Questions

resetting rotation on a single axis 2 Answers

Flip over an object (smooth transition) 3 Answers

calculating looking angle between 2 transforms 0 Answers

Particle System Instantiate's With Original Rotation - C# 1 Answer

2D Rotation of a Sprite - Quaternion.FromToRotation Smooth 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