Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Raycasting problems

Discussion in 'Scripting' started by AnipGames, Jan 6, 2021.

  1. AnipGames

    AnipGames

    Joined:
    Nov 27, 2018
    Posts:
    34
    Hello,
    I am trying to make a hex and culling system like in this article:
    https://www.gamasutra.com/blogs/DannyWeinbaum/20171201/310813/Foliage_Optimization_in_Unity.php
    I have made the hex-grid generation system which also groups objects into each hex. I currently am working on an occlusion and frustum culling system. The frustum check is working great, its the raycasting for occlusion culling that is driving me crazy. The thing is, whenever I check the code, it looks to me like it should work, but it sort of works. The worst part is that the raycasting works for a couple hexes, that's it, the rest it says are occluded even though they are not. All the hexes are generated the same, too so there cannot be any problems with it hitting something wrong. I have no idea whats going on so any help or hints as to what is wrong would be greatly appreciated.
    (also if anybody knows of an already-made system out there that I could use, please let me know)
    Here is a link to download the project for reference:
    https://drive.google.com/file/d/1OHAq6ZQ_TLiQyMwq18NBgbOlUii6qz92/view?usp=sharing
    Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    Also, if it sort of works, that's GREAT! Now thunder forth:

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  3. AnipGames

    AnipGames

    Joined:
    Nov 27, 2018
    Posts:
    34
    Thanks for the response!
    I actually did try putting a bunch of debug.log and print statements throughout the code to see what what happening. And when I run the code, the debug.logs say that nothing is going wrong. According to the debugs, the raycasts are hitting the correct object (which to the code, should mean that the hexes are not occluded) but the code only un-culls two of the hexes for some reason, not all of the others. There also were no errors in the console when I was running the code.
    I just dont know whats causing a couple of hexes to be revealed while the rest are still hidden for no reason.
     
  4. AnipGames

    AnipGames

    Joined:
    Nov 27, 2018
    Posts:
    34
    Ok, so I think I figured out the problem but I dont know what to do to solve it. It turns out that since it is a grid of hexes, when the camera does a raycast, the cast ends up hitting the collider that belongs to the hex in front of the one the raycast is trying to find. This is because I need the raycast to hit the hex colliders but that also means that the hex colliders in front of the goal will be hit, making it think that its occluded but its not. Would a Linecast possibly work?
    Sorry if this sounds confusing...
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Have you considered RaycastAll() ?
     
  6. AnipGames

    AnipGames

    Joined:
    Nov 27, 2018
    Posts:
    34
    No, I haven't actually. I didn't even think about that. I wonder, do you think that raycastAll would actually be more efficient than a linecast in this situation since I would need much more linecasts than raycastAll's?
    Thanks!
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Well that's looping back to first principles and something only the profiler can answer.

    This gets me back to your original post:

    Do I correctly assume you are doing this because a) you HAVE a performance problem, and b) you HAVE run the profiler, and c), the profiler HAS led you to identify that such a system would help you? Because Unity is a pretty smart engine all on its own about not rendering stuff out of view bounds...
     
  8. AnipGames

    AnipGames

    Joined:
    Nov 27, 2018
    Posts:
    34
    Ok, I just solved the problem!!
    I tried using raycastAll but I couldn't figure out how to iterate between all gameobjects so I tried using linecasts and it WORKED!!
    I just replaces raycast with linecast and then I checked to see if the object colliding was the terrain and if it was, I occluded the object.
    Thank you so much for the help!
     
    Kurt-Dekker likes this.