Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 chronocide · Nov 30, 2016 at 06:35 PM · terrainraycasting

How to make a raycast follow terrain?

So I have a case where I need a raycast to follow the ground. Why I need that is pretty complicated, so instead here's an analogy; let's say I have a hitscan gun whose bullets are always Y units above the ground, regardless of how steep etc the ground is. The bullets only travel X distance before disappearing. So basically I need a raycast to "follow" any hills and valleys that may be in the way.

One advantage is the ground will always be low poly, so there will be few but sudden changes instead of lots of gradual changes. However the ground is not tile based, so the length and angle of my ramps will vary.

One idea I had was to make all edges of my terrain have colliders which "redirect" the raycst, like so: alt text

but I don't know if that will be performant. Another challenge is that I'm going to be potentially making thousands of these calls in a frame.

Apologies if this question has been asked already, my searches mostly turned up people trying to make a vehicle's rotation follow terrain which isn't what I'm looking for.

sk-cd0bb182d320c1afb2dfbb521caf0812.jpeg (25.3 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 roman_sedition · Dec 04, 2016 at 04:50 AM 0
Share

I don't understand what kind of information you want back from the raycast if you could successfully implement it. Can you give a different analogy? Due to the behavior of the bullet it seems independent of raycasting whether the bullet hits the target or not because the bullet can only travel a set distance and requires path finding.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by christoph_r · Nov 30, 2016 at 10:58 PM

There's a couple of solutions to this. Generally, if your object moves sufficiently slow it's enough to do a raycast to the terrain to establish its height and adjust it accordingly. You could also use the normal vector obtained by the raycast hit to align your projectile's orientation with the terrain gradient. Make sure to only use the part of the normal vector that changes your projectile's pitch (and not its yaw.) An issue arises when your projectile/object moves very fast, in which case it might be "under the terrain" before it gets to raycast the terrain's elevation. This could be prevented by raycasting from a relatively high Y position, which should be a viable solution if you have only few of those projectiles at any given time.

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 chronocide · Nov 30, 2016 at 11:08 PM 0
Share

I have a feeling that would be too many raycasts, since I need potentially thousands of these rays drawn per frame. If each cast was constantly casting downwards too I feel like that wouldn't work. Also worth noting, in my example the "projectile" is hitscan, i.e. no travel time -- this all has to happen in one frame.

avatar image getyour411 chronocide · Dec 03, 2016 at 11:24 PM 0
Share

If the projectile is hitscan and nothing is really moving, why does it need to follow the terrain? 'Thousands of these rays per frame' sounds ambitious maybe you could elaborate on what you are actually trying to do here.

avatar image christoph_r · Nov 30, 2016 at 11:16 PM 0
Share

I see, I understood your question wrong then. You might want to look at reading your terrain's heightmap for its gradient data by marching from the firing position. Do a raycast until the first point where the heightmap marching detects a sufficient change in the gradient. If the raycast hit nothing, repeat while adjusting your raycast's direction.

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

69 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

Related Questions

Raycast up slope to find the peak in the terrain. 1 Answer

Please, I need help with Raycasting! 4 Answers

How do I determine what texture I am hitting on a terrain? 0 Answers

Make a simple tree 1 Answer

Terrain height raycasting? 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