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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by Linus · Aug 13, 2013 at 09:57 AM · fromtorotation

Correcting hit.normal when hit.transform is rotated

I am placing a number of planks/boards onto each other.

I place them using a raycast, and get them to stick to the surface the raycast hits.

The ghost prefab (a simple Unity box) used has its localTransform adjusted to align with the surface that the cross-hairs are pointed at.

It works fine as long as the what I am placing onto is rotated 0 or 90 degrees so that my if's catch the normals.

alt text

Can anyone help me with better code for the rotations.

The following code is inside a raycast:

 //sizeX,sizeY,sizeZ are the renderer dimensions for the object placed.
 
         ghostMaterials[ selectedMaterial ].transform.position = hit.point;
 
         ghostMaterials[ selectedMaterial ].transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
 
         if(hit.normal == Vector3(1.0,0,0) ){        
             ghostMaterials[ selectedMaterial ].transform.localPosition.x += (sizeX / 2 ) ;
         } else if( hit.normal == Vector3(-1.0,0,0)  ) {        
             ghostMaterials[ selectedMaterial ].transform.localPosition.x -= (sizeX / 2 ) ;            
         } else if( hit.normal == Vector3(0,0,1.0)  ) {
             ghostMaterials[ selectedMaterial ].transform.localPosition.z += (sizeZ / 2 ) ;            
         } else if( hit.normal == Vector3(0,0,-1.0)  ) {
             ghostMaterials[ selectedMaterial ].transform.localPosition.z -= (sizeZ / 2 ) ;    
         } else if( hit.normal == Vector3(0,-1.0,0)  ) {
             ghostMaterials[ selectedMaterial ].transform.localPosition.y -= (sizeY / 2 ) ;
         } else if( hit.normal == Vector3(0,1.0,0)  ) {
             ghostMaterials[ selectedMaterial ].transform.localPosition.y += (sizeY / 2 ) ;            
         }


rotation-issue.png (119.4 kB)
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
1
Best Answer

Answer by Jamora · Aug 13, 2013 at 05:53 PM

You need to use local axes instead of the world axes.

Check if the normal is parallel to any of the local axes, then decide if it's front or back.

 //up
 if( hit.normal == hit.transform.up ) {
 ghostMaterials[ selectedMaterial ].transform.localPosition.y += (sizeY / 2 ) ;
 }
 
 //down
 if( hit.normal == -1*hit.transform.up ) {
 ghostMaterials[ selectedMaterial ].transform.localPosition.y -= (sizeY / 2 ) ;
 }

For a more general situation, (or if you just want to get rid of the ifs) you should rotate the object-to-be-placed so that its up-vector is equal to the hit.normal, then modify the position with transform.localPosition.y += (sizeY / 2 ) ;.

Comment
Add comment · Show 4 · 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 Linus · Aug 14, 2013 at 10:10 AM 1
Share

I fail to understand. Been trying numerous combination of things, the correct one must be the only one left. It still only works if the hit.transform side has no rotation.

Can you/or anyone please explain how I get my ghost object to have same up as the hit.normal?

avatar image robertbu · Aug 18, 2013 at 09:02 AM 0
Share

Can you/or anyone please explain how I get my ghost object to have same up as the hit.normal?

Pseudo code:

 ghost.rotation = Quaternion.FromToRotation(ghost.up, hit.normal) * ghost.rotation;
avatar image Jamora · Aug 18, 2013 at 09:06 AM 0
Share

I mean you do this, or something similar in your Raycast

     transform.up = hit.normal;
     transform.position = hit.point;
     transform.position += transform.up*collider.bounds.extents.y;
avatar image Linus · Aug 18, 2013 at 09:54 AM 0
Share

Thank you so much @Jamora that worked

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

16 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

Related Questions

Can I use Quaternions to get from one point to another? 1 Answer

Mesh rotation issue ( using FromToRotation ) 0 Answers

Orientate a quad to the ray cast hit.normal 1 Answer

FromToRotation - Smoothly! 1 Answer

Question regarding - Quaternion.FromToRotation, Walking around a cube 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