Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 UberLou · Apr 27 at 03:55 PM · quaternionnormals

Player Rotating Oddly When Aligning to a Certain Normal

I have a slide over mechanic in my game where the player can slide over objects. When the slideover is initiated, the player will immediately rotate on the y axis to face the surface and then slide over it. Its generally working, however on a wall facing a particular direction, the hitInfo retuns (0,0,1) which flips the player -180 on the x axis. I'm unsure why this is happening as the rotation should be on the y axis. Any ideas on how to fix it?

 //Create a ray forwards from the player to detect any surfaces
  ray = new Ray(new Vector3(transform.position.x, transform.position.y + 0.25f, transform.position.z), transform.forward);
 RaycastHit hitInfo;
 //Rotate the player to face the surface if one is detected
 if (Physics.Raycast(ray, out hitInfo, 1, mask)) {
     transform.rotation = Quaternion.FromToRotation(Vector3.back, hitInfo.normal);
 }


Comment
Add comment · Show 2
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 jackmw94 · Apr 27 at 08:25 PM 1
Share

You are assigning your transform's orientation as a rotation.

Quaternions are like vectors in that they can either be an absolute position in space or an offset. If you want your transform to move 1 unit to the right from its current you can write transform.position += Vector3.right which is of course different from setting its position to be equal to Vector3.right. The former uses it as an offset and the latter as an absolute value. When used as an absolute value, it's essentially still an offset but from the origin point, (0,0,0).

The same is true of quaternions: they're either rotations, like our vector offset, or they're orientations, like our absolute position. When we assign a quaternion to our transform's rotation field, it's just saying this is the rotational change from a starting point of facing forward that this object needs to undergo. The FromToRotation function returns us a rotation, used to multiply an existing orientation in order for it to undergo the same rotational transform as required to get from the first direction argument you pass to FromToRotation to the second.

If you feel this is enough information to have another go then my final tip is that I feel as though your solution will either require Quaternion.LookRotation if you want to specify the direction in which your rotation is looking or will require a nice old Quaternion.AngleAxis if you want to explicitly specify the axis around which your transform rotates. I can give you more help if you can post a little more information about your problem, a screenshot might be useful just showing what axes of your object need to orient to which directions relating to the slide object.

Hope this is some help :)

avatar image UberLou jackmw94 · Apr 28 at 02:07 PM 1
Share

Really appreciate the in depth explanation! I got it working using

 transform.rotation = Quaternion.LookRotation(hitInfo.normal);
 transform.rotation *= Quaternion.Euler(0, 180f, 0);

Thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Sacaitha · Apr 27 at 09:04 PM

Use Quaternion.LookRotation

Comment
Add comment · 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

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

139 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

Related Questions

Transform rapidly spins when flipped upside down. 2 Answers

Move object along the curved surface. Problem moving to bottom hemisphere. 0 Answers

Calculate Rotation based on mesh normals 1 Answer

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

Comparing a quaternion to a contact point normal 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