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 /
avatar image
0
Question by Ryan IRL · Aug 17, 2010 at 07:31 PM · rotationterrainquaternionangleclamp

Align GameObject to Terrain angle

I'm trying to enhance a simple clamp to terrain script I wrote to also rotate the object based on the angle of the terrain under it. There is probably easier ways to do what I am trying to do, but I'd also like to know why my code is simply not working.

function AlignToTerrainAngle(pos : Vector3, unitSize : float = 1.0) { var frontSide : Vector3 = Vector3(pos.x + unitSize, 0, pos.z); frontSide.y = terrain.SampleHeight(frontSide);

var backSide : Vector3 = Vector3(pos.x - unitSize, 0, pos.z); backSide.y = terrain.SampleHeight(backSide);

var leftSide : Vector3 = Vector3(pos.x, 0, pos.z + unitSize); leftSide.y = terrain.SampleHeight(leftSide);

var rightSide : Vector3 = Vector3(pos.x, 0, pos.z - unitSize); rightSide.y = terrain.SampleHeight(rightSide);

// get pitch angle var pitchAngle : float = Vector3.Angle(frontSide, backSide);

// get yaw angle var yawAngle : float = Vector3.Angle(leftSide, rightSide);

Debug.Log("Pitch angle: " + pitchAngle + " Yaw angle: " + yawAngle);

var newRotation : Quaternion = Quaternion.identity; newRotation += Quaternion.AngleAxis(pitchAngle, Vector3.right); newRotation += Quaternion.AngleAxis(yawAngle, Vector3.forward);

// Rotate the model! transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 2.0);

}

I feel like I'm pretty close, but the angles are way too small (< 0.3), and assigning the new rotation Quaternion to the transform object seems to do nothing. Even hard coding larger angles resulted in no change.

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

2 Replies

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

Answer by Wolfram · Aug 17, 2010 at 09:35 PM

The Slerp interpolation will only work if this function is called repeatedly (i.e., in Update()), since this variant of using a Lerp function (by giving it Time.deltaTime as interpolation parameter) is more like a "smooth follow", no longer a linear inerpolation. You might also want to increase the 2.0 scaling, to influence the speed at which the new rotaion angle is adapted.

Note that there is a method called Terrain.terrainData.GetInterpolatedNormal(x,y) which gives you the normal at a given position. However, since it is a method of the heightmap data, the values x and y are normalized between 0 and 1, so you would have to compute them by yourself, using your pos.x and pos.z and the actual terrain dimensions+location.

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

Answer by BLF-Games · Jun 22, 2011 at 03:21 PM

I know this is old, but it may help others as it helped me:

 //make platform adjust terrain rotation
 var rcHit : RaycastHit;

 //Make raycast direction down
 var theRay : Vector3 = transform.TransformDirection(Vector3.down);
 
 if (Physics.Raycast(transform.position, theRay, rcHit))
 {
     //this is for getting distance from object to the ground
     var GroundDis = rcHit.distance;

     //with this you rotate object to adjust with terrain
     transform.rotation = Quaternion.FromToRotation(Vector3.up, rcHit.normal);

     //finally, this is for putting object IN the ground
     transform.localPosition.y = (transform.localPosition.y - GroundDis)+1;
 }
Comment
Add comment · Show 2 · 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 Ricane · Jul 20, 2014 at 10:26 AM 0
Share

Great, works perfect! Thanks a lot.

avatar image chanfui · Oct 24, 2017 at 06:14 AM 2
Share

Thank you. Your answer probably causes butterfly effect and save the world from alien invasion.

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

2 People are following this question.

avatar image avatar image

Related Questions

Rotating an object so that the direction between it's children matches the forward of rotating object 2 Answers

Head and body rotate threshold 1 Answer

Applying a clamp to Quaternion.LookRotation 1 Answer

Rotate floor plane in-game via C# script 1 Answer

Set a rotation relative to position between two objects. 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