Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 PvTGreg · Jan 18 at 08:04 PM · rotationterrainvector3

Rotate Object to match terrain Slope

I am trying to a fence object to rotate to match the terrian. i can get this to work by rotating the object using the results of one raycast but when i rotate the object later on the y axis it starts to rotate the wrong way. so i was trying to instead use two raycasts and rotate it to match the normals of those. but i cant seem to get it to work at all. it just seems to rotate wildly and im not sure where i am going wrong. below is my current code and a picture of what i am trying to acheive

  if (matchTerrain)
                 {
                     RaycastHit Rhit;
                     if (Physics.Raycast(rightRay.position, -Vector3.up, out Rhit))
                     {
                         RaycastHit Lhit;
                         if (Physics.Raycast(leftRay.position, -Vector3.up, out Lhit))
                         {
                             //Vector3 upright = Vector3.Cross(itemClone.transform.right, -(Rhit.point - Lhit.point).normalized);
                             //itemClone.transform.rotation = Quaternion.LookRotation(Vector3.Cross(itemClone.transform.right, upright));
 
                             //Vector3 upright = Vector3.Cross(transform.right, -(Rhit.point - Lhit.point).normalized);
                             //itemClone.transform.rotation = Quaternion.LookRotation(Vector3.Cross(transform.right, upright));
 
                             Vector3 upright = Vector3.Cross(itemClone.transform.up, -(Rhit.point - Lhit.point).normalized);
                             Quaternion newRot = Quaternion.FromToRotation(itemClone.transform.up, upright) * itemClone.transform.rotation;
                             itemClone.transform.rotation = newRot;
 
                             if (addToRotation)
                             {
                                 itemClone.transform.rotation = itemClone.transform.rotation * rotationChange;
                             }
                         }
                     }
                 }

alt text

screenshot-2022-01-18-195830.png (337.8 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 PvTGreg · Jan 21 at 05:14 PM 0
Share

Still stuck with this tried various solutions they all dont work. what i wat to achieve is the y axis is untouched but can be changed later and the x axis rotates to match the terrain normal things ive tried below

  //Vector3 upright = Vector3.Cross(itemClone.transform.right, -(Rhit.point - Lhit.point).normalized);
                             //itemClone.transform.rotation = Quaternion.LookRotation(Vector3.Cross(itemClone.transform.right, upright));
 
                             //Vector3 upright = Vector3.Cross(transform.right, -(Rhit.point - Lhit.point).normalized);
                             //itemClone.transform.rotation = Quaternion.LookRotation(Vector3.Cross(transform.right, upright));
 
                             //Vector3 upright = Vector3.Cross(itemClone.transform.up, -(Rhit.point - Lhit.point).normalized);
                             //Quaternion newRot = Quaternion.FromToRotation(itemClone.transform.up, upright) * itemClone.transform.rotation;
                             // itemClone.transform.rotation = Quaternion.Euler(newRot.x, transform.rotation.y, itemClone.transform.rotation.z);
 
 
                             //works but is rotated the wrong way 
                             //Vector3 upright = Vector3.Cross(itemClone.transform.up, -(Rhit.point - Lhit.point).normalized);
                             // itemClone.transform.rotation = Quaternion.LookRotation(Vector3.Cross(itemClone.transform.up, upright));
 
 
                             //works but cant rotate
                             //itemClone.transform.up = hit.normal;
 
                             //Vector3 upright = Vector3.Cross(itemClone.transform.up, hit.normal);
                             //itemClone.transform.rotation = Quaternion.LookRotation(upright);
 
                             //itemClone.transform.rotation = Quaternion.LookRotation(transform.up, hit.normal);
 
                             //itemClone.transform.rotation = Quaternion.LookRotation(itemClone.transform.transform.up, hit.normal);
                             // Quaternion originalRot = itemClone.transform.localRotation;
                             //itemClone.transform.localRotation = Quaternion.Euler(hit.normal.x, transform.localRotation.y, originalRot.z);
                             //Vector3 newRot = new Vector3(hit.normal.x,transform.rotation.y,itemClone.transform.rotation.z);
                             //itemClone.transform.rotation = Quaternion.LookRotation(itemClone.transform.eulerAngles, newRot);
 
                             //Vector3 originalRot = itemClone.transform.eulerAngles;
                             //itemClone.transform.rotation = Quaternion.LookRotation(itemClone.transform.up, hit.normal);
                            // itemClone.transform.rotation = new Quaternion(itemClone.transform.rotation.x,originalRot.y,originalRot.z,0);

1 Reply

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

Answer by PvTGreg · Jan 21 at 05:34 PM

Not sure how this works my brain is fried but i got it working using this incase anyone else is interested

 Vector3 upright = Vector3.Cross(itemClone.transform.right, -(Rhit.point - Lhit.point).normalized);
                             itemClone.transform.rotation = Quaternion.LookRotation(Vector3.Cross(itemClone.transform.right, upright));
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

222 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

Related Questions

How to make flat object(Unity 2D) 1 Answer

Vector3.forward only moves me in 2 directions when rotating 1 Answer

Terrain tree painter lays trees horizontally on the ground 1 Answer

Checking vector3 gameObject is about to move to (pathfinding collision detection) 1 Answer

Why do grouped Terrain objects move together but don't rotate as one? 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