Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 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 Junglerally · Jan 07 at 11:30 PM · rotationbugvector3quaternionprogramming

Problem with normal snapping and rotations.

I have a game where you can build mini-golf holes and place things on them, and I wanted to make it so that the objects you are placing match up with the normals of the hole because there might be an elevation change in the course. I have the hole "decorating" or object-placing script here, but I will show the main part of the code that is doing it below.

So to start, I set the position of the object to another object which moves to the mouse's position on the ground.

 if (!isRotating)
 {
     //Movement
     pendingObject.transform.position = buildingPosObj.transform.position;

(The isRotating bool is for another form of rotation, I have two. The first is just pressing R to rotate the object 45 degrees, the second is holding R to freely rotate it, and it shouldn't move when freely rotating it)

That part works fine. But the next part is what is causing the problem. If you would like to see the bug, click here. This is the part of the code that makes the rotation:

  Vector3 up = hit.normal;
                 Vector3 forward = Vector3.Cross(Vector3.up, up);
                 Vector3 finalRot = new Vector3(Quaternion.LookRotation(forward, up).eulerAngles.x, Quaternion.LookRotation(forward, up).eulerAngles.y + pendingObject.transform.eulerAngles.y, Quaternion.LookRotation(forward, up).eulerAngles.z);
                 pendingObject.transform.eulerAngles = finalRot;

So I know the problem is because I am adding the new rotation to the old one, but I don't really know any efficient and clean ways to fix this. I have tried one thing, but it still adds it to the rotation whenever the normal changes:

             if(hit.normal != previousNormal)
             {
                 previousNormal = hit.normal;
                 Vector3 up = hit.normal;
                 Vector3 forward = Vector3.Cross(Vector3.up, up);
                 Vector3 finalRot = new Vector3(Quaternion.LookRotation(forward, up).eulerAngles.x, Quaternion.LookRotation(forward, up).eulerAngles.y + pendingObject.transform.eulerAngles.y, Quaternion.LookRotation(forward, up).eulerAngles.z);
                 pendingObject.transform.eulerAngles = finalRot;
             }

This didn't work. I am assuming that I should change how I am doing the normal snapping entirely, but I have no clue how to go about this. Any help would be appreciated, even anywhere else in my script you see problems.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Junglerally · Jan 08 at 09:07 PM

FIXED: I made a new method for returning the vector3 and used the same if statement. The new method has some checks to see if the normal is positive or negative and returns different values for that. Here is the new method for anyone interested:

     Vector3 GetObjectRot()
     {
         Vector3 finalRot;
         Debug.Log(hit.normal);
             previousNormal = hit.normal;
             Vector3 up = hit.normal;
             Vector3 forward = Vector3.Cross(Vector3.up, up);
             if (hit.normal.x < 0)
             {
                 finalRot = new Vector3(Quaternion.LookRotation(forward, up).eulerAngles.x, pendingObject.transform.eulerAngles.y, Quaternion.LookRotation(forward, up).eulerAngles.z);
             }
             else
             {
                 finalRot = new Vector3(Quaternion.LookRotation(forward, up).eulerAngles.x, pendingObject.transform.eulerAngles.y, Quaternion.LookRotation(forward, up).eulerAngles.z*-1);
             }
             return finalRot;
     }
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

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

Multiple Cars not working 1 Answer

Rotate an object so its up vector matches a sphere 1 Answer

Rotate object so part of it faces another direction 0 Answers

Get rotation from a vector direction. 1 Answer

3D nested Turret Prefab Rotation 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