Search code examples
c#androidunity-game-enginegame-physicsraycasting

Trouble with RayCasting to check neighboring blocks for a specific state


Sorry for the long question. I am making a simple game where you click a cube and it changes its material to a certain color(i.e. dirt/grass >> water and any dirt "tiles" next to the water tile(cardinally) will automatically become yellow). These colors are kept by using an int value to identify the state.

I failed at using an 2d array in c# to store these cubes so I was attempting to use raycasting to check for these neighboring relationships. All the cubes are on a single plane of y=0 and they have a l=1m.

Here is the code for checking the neighbors.enter image description here

Here is the starting gameboard with all tiles turned into dirt. This means When I click a tile it should turn blue(which is working) and then surrounding brown tiles should change their state to 3 therefore turning them yellow. Here, however, is what happens. Initial:enter image description here

After clicking one tile:enter image description here

I added some debugging console writes to try and help but had no personal luck. Here they are:enter image description here

Thank you for any help you can give. I'm new to c# and am having a lot of trouble!


Solution

  • By changing my raycast to

    check(tr,transform.forward)
    

    Instead of

    check(tr,(tr+transform.forward))
    

    I was able to cast the rays from the proper position. Still a little confused about it but it works.