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 /
avatar image
1
Question by TehJellyLord · Jan 04, 2020 at 08:28 PM · rotationraycastrotate objectsurface normal

Is there a fix for ground normals rotates my player in unwanted ways?

I'm trying my hand at a Sonic Fan game, something I've wanted to do for years now, but I'm having issues with rotating my character based on the normals of the ground.

Before I say anything, I will clarify that I have rotated everything so that +Z is Upward, -Z is Downward, -Y is Forward (imagine the Y-axis is me pointing a finger at my screen), and +Y is Outward. Also, I'm using a 2D sprite in a 3D space.

In my testing I have found that, for whatever reason, rotating Vector3.up towards the normal where my raycast hits, rotates it almost in the way I want, but it is also changing my X rotation in the inspector to 45 degrees (it needs to be 90, otherwise my sprite will look squished as seen in the screenshots bellow). Is there a way to make sure that the X rotation in the inspector is ALWAYS locked to 90 degrees, making sure there is no possible way for that to change?

Here is the the code that handles this.

int layerMask = 0; layerMask = ~layerMask;

     RaycastHit hit;
     if (Physics.Raycast(rb.position, rb.transform.TransformDirection(Vector3.down), out hit, 0.5f, layerMask))
     {
         Quaternion temp = Quaternion.FromToRotation(Vector3.up, hit.normal);
         onGround = true;
         transform.rotation = Quaternion.Slerp(transform.rotation, temp, Time.deltaTime * 10f);
     }
     else
     {
         onGround = false;
         transform.rotation = Quaternion.FromToRotation(Vector3.up, new Vector3(0, 0, 1));
     }

[1]: /storage/temp/150826-screenshot-38.png

[2]: /storage/temp/150826-screenshot-39.png

screenshot-39.png (278.3 kB)
screenshot-38.png (288.9 kB)
Comment
Add comment · Show 1
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 Ady_M · Jan 04, 2020 at 10:01 PM 0
Share

Out of curiosity, why did you rotate everything so Z is up?

Should your character be able to turn and run along the Y axis? (even though the camera always shows his side)

If not, could you try this: { Vector3 normal$$anonymous$$odified = new Vector3(hit.normal.x, 0, hit.normal.z); Quaternion temp = Quaternion.FromToRotation(Vector3.up, normal$$anonymous$$odified); onGround = true; transform.rotation = Quaternion.Slerp(transform.rotation, temp, Time.deltaTime * 10f); }

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

195 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 Do I add rotations to one of my objects in my AR application? 1 Answer

Look At Player with Clamping. 1 Answer

rotation with eulerangles and raycasting. 1 Answer

Translate speed keeps changing 1 Answer

Can't figure out how to make my raycast obstacle avoidance less jittery 3 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