• 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 xrad · Aug 27, 2017 at 08:58 PM · meshvector3mathangle

How do I find slope on a spherical terrain

I have a generated spherical mesh terrain with collider and I need to find if the slope of a triangle at a given point is too steep. I want to only find points with slope close to flat terrain. Basically I need to compare the normal of the triangle to something, however comparing it to Vector3.up doesn't quite do the trick. And it also returns angles greater than 90, I would expect angles to be only on the range [0, 90]. Don't need solutions involving raycasting. There must be a way to do it with just vectors.

Comment
Add comment · Show 2
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 Bunny83 · Aug 27, 2017 at 09:04 PM 0
Share

Way to unspecific question. What exactly do you understand by "sphere with terrain"? How is that terrain generated? How is it represented? Is it a mesh or a collection of meshes? Do the mesh objects have a mesh collider?

avatar image xrad Bunny83 · Aug 27, 2017 at 09:32 PM 0
Share

updated the original post

2 Replies

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

Answer by Bunny83 · Aug 27, 2017 at 10:23 PM

So if I understand your question correctly you basically have a large sphere mesh with uneven surface which probably resembles a planet like object. What you want to know is the angle between the surface normal of your terrain compared to the normal of the reference sphere at the same point.

The normal of the reference sphere can simply be gathered by taking the worldspace position on the surface of your object and subtract the center of your sphere and normalize the result.

Now you just need to use Vector3.Angle between that reference normal and your surface normal. You can get the surface normal by using a raycast against the MeshCollider of your sphere mesh.

Here's an image that shows what i mean:

sphere terrain normal


sphereterrainnormal.png (5.4 kB)
Comment
Add comment · Show 3 · 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 xrad · Aug 27, 2017 at 11:13 PM 0
Share

I think you got the idea, but not sure if I understand what you mean. So you are saying having the normal of the point on terrain I find:

v1 = normal - centre

then i compare Vector3.Angle(v1.normalized, normal.normalized)? Tried it and doesn't quite work.

avatar image Bunny83 xrad · Aug 27, 2017 at 11:44 PM 0
Share

No, you should do:

 refNormal = (p - center).normalized;

and then just do

 Vector3.Angle(refNormal, surfaceNormal)

p is the point on the terrain in worldspace coordinates from which you want to know the normal of. and surfaceNormal is the normal vector you usually get from a raycasting onto a $$anonymous$$eshCollider. (hit.normal).

avatar image xrad · Aug 28, 2017 at 10:09 AM 0
Share

Thanks a lot, it finally worked. I was just not normalizing the whole (p - centre) and in my case I had to do it with Quaternions. And I was stupidly not realizing that flat terrain means angle of 0 between the vectors.

avatar image
0

Answer by unidad2pete · Aug 27, 2017 at 09:57 PM

If you need get a Vector3 on mesh , I dont have idea how do it without raycasting.

But with only one Linecast you can do it.

 public LayerMask yourLayer;
     public float distanceToYourFoot; // the distance of your player to you foot (if your player scale is Vector3(x, 2, z) your position is (x, 1, z) distance = 2 (high) - 1 (position) 
     RaycastHit hit;
     float angle;
     private void Update()
     {
 
         if(Physics.Linecast(transform.position + transform.forward + Vector3.up * 100, transform.position + transform.forward - Vector3.up * 10, out hit, yourLayer))
         {
             Debug.DrawLine(transform.position + transform.forward + Vector3.up * 100, transform.position + transform.forward - Vector3.up * 10, Color.red); //Print the line of the LineCast
             Debug.DrawLine(transform.position + Vector3.down * distanceToYourFoot, hit.point, Color.green); // Print a Line starting on your position,  to direction you are loocking arround the terrain.
 
 
             angle = Vector3.Angle(hit.point - transform.position + Vector3.down * distanceToYourFoot, transform.forward);
 
             // Value 45 represent just a straight line , the floor is plane, value 90 is a wall 
             // You can get your value based on 0 with 
             // angle -= 45; Now 0 is plane, 45 is a wall
 
             print(angle);
         }
     }

You only need adjust values to your preferences of distances to check and directions.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

90 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

Related Questions

Angle between two lines in 2D 3 Answers

Result of subtracton vectors 1 Answer

Find Vector3 from a starting Vector3, angle, and distance. 1 Answer

Vector3.Angle returning wrong values for vectors with small components 2 Answers

Get angle relative to GameObject 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