Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tiles swapping for wall drawing based on raycast.

Discussion in 'Scripting' started by ikazrima, Mar 29, 2014.

  1. ikazrima

    ikazrima

    Joined:
    Feb 11, 2014
    Posts:
    320
    Hi all,

    I'm trying to code a wall drawing algorithm where the side tiles will change whether there's is a tile next to it or not. Basically you will see this effect on RPG Maker tiles drawing or any other similar projects.

    Here's my illustration :-

    View attachment 93467

    Due to certain limitation I cannot store my walls in an array or other grid based approach.

    My current solution is to draw all walls as the centre tile, and then cast raycast to the left and right and see whether there is a wall next to it.
    If there's a wall on the left and right, do nothing.
    If there's no wall on left or right side, change tiles.

    I've added a box collider to the wall object. The problem is, the raycast keeps hitting its own collider so that the hit always returns a wall object. Note that I cannot change the layers into ignoreraycast as the wall objects are derived from the same prefab.

    Is there's is any way that I can ignore the raycast on its self, or does any of you have any other method on solving this problem?
     
  2. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
    One solution would be to use "RaycastAll" - and ignore 'self' when processing the results.
     
  3. ikazrima

    ikazrima

    Joined:
    Feb 11, 2014
    Posts:
    320
    Hi Jakob,

    How do I actually do this? I've googled around and can't seem to find out how. I've read somewhere in the forum that I need to ignore the first object hit since that's almost/always the object's own collider. How do choose which object to ignore without using layers?

    Thanks for your reply.

    *I've changed a bit on how my tilings work (by using array) and managed to solve the case without using raycast. However it comes with performance cost, where previously each level loads instantaneously while right now it takes about 4-5 seconds to load (I know it's not that long, but if I can optimize it better why not)
     
  4. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
    FWIW - I'd expect array indexing to beat raycasts at any time for this kind of task.

    Anyway - How about something like this:

    Code (csharp):
    1.  
    2.     void RaycastIgnore (Ray ray, Transform ignore, float maxDistance)
    3.     {
    4.         RaycastHit[] hits;
    5.         hits = Physics.RaycastAll(ray, maxDistance);
    6.  
    7.         for (int i = 0; i < hits.Length; ++i) {
    8.             if (hits[i].transform == ignore)
    9.                 continue;
    10.  
    11.             Debug.Log ("Hit : " + hits[i].transform); //< do stuff here
    12.         }
    13.     }
    14.  
    On the other hand. You know that you should ignore one hit always (since you start inside a collider always) - then you also know that when hits.Length > 1 - you've hit something else. and if hits.Length == 1 you've hit self.