• 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
Question by SteenPetersen · May 19, 2020 at 11:24 AM · rotationquaternioneuleranglesnormals

(Solved) Aligning player to surface while still maintaining look direction

Let's assume I have a sphere that can hover, so it has an offset from the ground. What I want is to simply rotate the sphere to match the normals of whatever is under it on the X and Z-axis, but still be able to rotate it to the direction in which it is travelling on the Y-axis. I have tried a few things that I found here on the forum and [2] seemed promising but unfortunately, it did not work.

 RaycastHit hit;
 if (Physics.Raycast(body.position, Vector3.down, out hit, 10, layerMask))
 { }   
 
 Quaternion groundTilt = Quaternion.FromToRotation(Vector3.up, hit.normal);
 body.rotation = Quaternion.Euler(0, currentMovementDirection.y, 0);
 body.rotation = groundTilt * body.rotation;


Here is a diagram of what I want, seen from top and side views. alt text

references: [1] https://answers.unity.com/questions/1347986/rotating-a-player-to-match-terrain-slope.html [2] https://forum.unity.com/threads/rotating-an-object-along-the-terrains-normal-whilst-looking-at-a-position.93570/

movement.png (41.0 kB)
Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by Namey5 · May 19, 2020 at 11:55 AM

You can use vector projection to find the relative forward vector between the ground's normal and your movement vector;

 if (Physics.Raycast (body.position, Vector3.down, out RaycastHit hit, 10f, layerMask))
 {
     //Up is just the normal
     Vector3 up = hit.normal;
     //Make sure the velocity is normalized
     Vector3 vel = currentMovementDirection.normalized;
     //Project the two vectors using the dot product
     Vector3 forward = vel - up * Vector3.Dot (vel, up);
 
     //Set the rotation with relative forward and up axes
     body.rotation = Quaternion.LookRotation (forward.normalized, up);
 }
Comment
SteenPetersen

People who like this

1 Show 3 · 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 SteenPetersen · May 19, 2020 at 03:27 PM 0
Share

Hey there Namey thanks for your answer I've been reading up on your answer and although it seems to be correct I am getting unexpected results. Due to the nature of what I'm trying to do I need to rotate quite slowly.

I have used the following code:

 Quaternion newRot;
 
 if (Physics.Raycast(body.position, Vector3.down, out RaycastHit hit, 100f, layerMask))
 {
    Vector3 up = hit.normal;
    Vector3 vel = currentMovementDirection.normalized;
    Vector3 forward = vel + up * Vector3.Dot(vel, up);
 
    newRot = Quaternion.LookRotation(forward.normalized, up);
    body.rotation = Quaternion.RotateTowards(body.rotation, newRot, bodyRotationSpeed * Time.deltaTime);
 }


And what is happening is that he moves like Id expect while on a flat surface but when he gets to a ramp he seems to turn in the opposite direction. if you take my diagram from the OP the red dot starts to point down instead of up when entering the ramp.

avatar image Namey5 SteenPetersen · May 20, 2020 at 12:48 AM 0
Share

Whoops, I accidentally swapped the order of the projection - it was supposed to be a subtraction;

 Vector3 forward = vel - up * Vector3.Dot (vel, up);

I've also edited the original.

avatar image SteenPetersen Namey5 · May 20, 2020 at 06:02 AM 0
Share

Ahh, of course, I should have thought of that, I thought it was somehow my geometry that was off and was seeing if I could launch the ray diffrently.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

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

How to handler rotation of a Gameobject while aligning it with normals 1 Answer

How do you recreate unity's rotation gizmo? 0 Answers

Apply game object rotation using relative VR controller rotation? 2 Answers

Rotate Player to Match Surface Normal While Still Maintaining Y Rotation 0 Answers

How to get Angle float of specific axis.(Turret clamping related). 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