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 Skaster87 · Mar 09 at 05:56 AM · terrainraycastingslope

Raycast up slope to find the peak in the terrain.

Hi,

I'm trying to programmatically find the peak of a terrain slope by incrementally raycasting up the slope until I don't hit the terrain any more, and I'm having trouble remembering the trig function to figure out how to find a spot to raycast from.

I'm raycasting downward evenly across the terrain, and storing the locations where the hit.normal angle is greater that 30 degrees, then looping through those points to find the approximate peak of the slope. So the blue line is the ray down, the green line is the normal, and I'm trying to determine the Vector3 of the orange arrow and the direction for the Red raycast back to the terrain.

alt text

To find the peaks in the terrain like here:

alt text

So basically I'm trying to remember how to find a spot on the green line that corresponds to creating a right angle on the blue line.

I have:

     private void CreateTerrainLedges(List<Vector3> points)
     {
         foreach (var point points)
         {
             bool isPeak = true;
             var hits = new List<RaycastHit>();
             var rayOrigin = point + Vector3.up; // + whatever offset for the normal
             var dir = new Vector3(point.x, point.y + Vector3.up, point.z);
             var rayDirection = (dir - rayOrigin).normalized;
             var ray = new Ray(rayOrigin, rayDirection);
             var top = RayToTop(ray, hits); // recursively raycast to find approx top
             // do stuff with the top point
     }
 
     private RaycastHit RayToTop(Ray ray, List<RaycastHit> hits)
     {
         if (Physics.Raycast(ray, out RaycastHit hit, 2.5f, gridLayerMask))
         {
             hits.Add(hit);
             var rayOrigin = new Vector3(hit.point.x, hit.point.y + .2f, hit.point.z);
             Ray newRay = new Ray(rayOrigin, ray.direction);
             return RayToTop(newRay, hits);
         }
         return hits.Last();
     }


peaks.png (438.0 kB)
raycast-peaks.png (356.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 sacredgeometry · Mar 09 at 08:51 AM

Here is an explanation using that methodology (i.e. the first part of the experiment)

https://youtu.be/ckcdqlo3pYc?t=172

or

https://owlcation.com/stem/How-to-Determin-the-Radius-of-the-Earth-Al-Birunis-Classic-Experiment


That said: Given that that you have more data than that available I probably wouldn't use that but instead use an actual hillclimbing algorithm or if its static just cache the local maximas and then derive them from closest distance.


Or you know? Raycast downwards from vertical instead and use the position of the first ray hit?

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

129 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

Related Questions

How do you set foot IK to terrain height? 1 Answer

Attempting to change the slope of a board to match the slope of the terrain below it 1 Answer

How to make a raycast follow terrain? 1 Answer

Nav mesh holes with terrain 1 Answer

Please, I need help with Raycasting! 4 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