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 larumerumba · Jul 03, 2013 at 01:00 PM · raycastterrainraycasthit

Raycasthit results only 0

Hello,

I was looking for this problem in other threads but yould not identify anything similar. What I want to do is the following: On pressing button "p" a plane shall appear on the surface of my terrain. I can choose in advance the x and z value; the y value shall be estimated with a raycast. The code for the raycast is standard:

 //---------------------------------------------------------------------
     // start and end of an orthographic ray that hits the terrain and tells us the elevation
     private static float rayTestTerrain(float xPos, float zPos, RaycastHit rayYPosHit)
     {
         Vector3 startRayPoint = new Vector3( xPos, 1000f, zPos );
 
         Ray orthoTestRay = new Ray( startRayPoint, Vector3.down );
         //Debug.DrawLine( startRayPoint, endRayPoint, Color.red, 10, false );
 
         RaycastHit lala = new RaycastHit();
 
         // actual ray test
         Physics.Raycast( orthoTestRay, out lala, Mathf.Infinity, U3D_ModelManager.m_TerrainLayerMask );
 
         return lala.point.y;
     }

The result, though, is always 0:

(139.9, 0.0, 171.6) UnityEngine.Debug:Log(Object)

Do you have any hints for me where to trace the error? The surface is a terrain, but I also created a simple plain on top to see if it gets a hit there, but negative...

Cheers!

Comment
Add comment · Show 4
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 larumerumba · Jul 03, 2013 at 12:23 PM 0
Share

sorry, a small correction, the plane DOES give a result, but not the terrain...

avatar image Benproductions1 · Jul 05, 2013 at 04:13 AM 0
Share

Looking at what other people posted, why are you using a raycast? Why not just have all your terrains in a list. Loop through that list. Check if your coordinates are within the bounds of the terrain (just some simple math) and then SampleHeight?

avatar image larumerumba · Jul 06, 2013 at 05:13 PM 0
Share

thank you a lot, it is working :-)

avatar image Benproductions1 · Jul 07, 2013 at 01:07 AM 0
Share

If it's working, either accept the answer that answered the question. Answer it yourself. Or close it :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dorpeleg · Jul 03, 2013 at 01:01 PM

Instead of using raycast, you can use this:

Terrain.SampleHeight

But remember, this is only for terrains.

Comment
Add comment · Show 14 · 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 dorpeleg · Jul 03, 2013 at 02:21 PM 0
Share

Weird, I tryed converting your answer to a comment (next time, you should post a comment and not a new answer) and it made it disappear :S

So if you can post agian as a comment....

avatar image larumerumba · Jul 03, 2013 at 02:25 PM 0
Share

uhm, sorry. thank you for your answer I said I need to use several terrains because I have a huge texture that I have to split. So I cannot tell which terrain to get the SampleHeight from (except quite a lot of effort to identify it)...

avatar image dorpeleg · Jul 03, 2013 at 02:29 PM 0
Share

Why not? If you can explain a bit more.... If you really have to use ray cast, I think you need to do it like so:

 if (raycat here)
 {
 (get your y value)
 )
avatar image larumerumba · Jul 03, 2013 at 02:38 PM 0
Share

I try to explain more: there are 9 terrains arranged to a square. The user gives the x and y value where to create my object. That could be anywhere in the scene on top of any of the 9 terrains and I try to avoid to make a test to see at which terrain it is as long as there is a solution that safes me the time and the code and actually it should work... :-/

I did the raycast as shown in the code on top only without the if statement, but the resulting y value is always 0

avatar image dorpeleg · Jul 03, 2013 at 02:40 PM 0
Share

That's what I mean, I think you need the if statement.

You can also raycast just to see which terrain you are above and then use the SampleHeight.

Show more comments

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

17 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

Related Questions

Raycasting Through Terrain 1 Answer

Rays do not detect terrain and prefabs do not spawn where wanted 1 Answer

Tree how to add a tag to it when it is apart of the terrain -1 Answers

Help Can't find the Correct Parameters 1 Answer

Selecting objects using raycasting 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