Raycast only hits the layer when hitting top of cube

hello there :slight_smile:

i currently have 2 layermask, Ground and Building.
Ground layer is the green area,
Building layer is the cube.
The red shows the raycasts.

1981-raycastcube.png

This is my current code:

     using UnityEngine;
using System.Collections;

public class mapinfo : MonoBehaviour 
{
 // Mapinfo and mouse position.
 public static int marker_x;
 public static int marker_z;
 
 public Ray ray;
 public RaycastHit hit;
 
 public int max_tiles;
 public int max_side = 8;
 
 public int [,] tiles = new int[10000,4];
 public int tile_number;
 public int tile_type;
 public int tile_ID;
 public int tile_owner;
 
 public LayerMask Ground = 1 <<8;
 public LayerMask Player_Land = 1 <<9;
 public LayerMask Player_Building = 1 <<10;
 
 public int Active_Layer = 0;
 
 public Transform house;
 
 // Use this for initialization
 void Start () 
 {
 
 }
 
 // Update is called once per frame
 void Update () 
 { 
 ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
 if (Physics.Raycast(ray, out hit, 200))
 {
 Debug.DrawLine(ray.origin, hit.point,Color.red,1f); 
 }
 
 if (Input.GetKey(KeyCode.Mouse0) && Active_Layer == 0)
 {
 
 if (Physics.Raycast(ray, out hit, 200,Ground))
 {
 Debug.DrawLine(ray.origin, hit.point,Color.red,10f);
 marker_x = Mathf.CeilToInt(hit.point.x);
 marker_z = Mathf.CeilToInt(hit.point.z);
 Active_Layer = 1;
 }
 
 if (Physics.Raycast(ray, out hit, 200,Player_Land))
 {
 Debug.DrawLine(ray.origin, hit.point,Color.red,10f);
 marker_x = Mathf.CeilToInt(hit.point.x);
 marker_z = Mathf.CeilToInt(hit.point.z);
 Active_Layer = 2;
 }
 
 if (Physics.Raycast(ray, out hit, 200,Player_Building))
 {
 Debug.DrawLine(ray.origin, hit.point,Color.red,10f);
 marker_x = Mathf.CeilToInt(hit.point.x);
 marker_z = Mathf.CeilToInt(hit.point.z);
 Active_Layer = 3;
 }
 }
 max_tiles = max_side*max_side;
 
 tile_number = marker_z*max_side+marker_x;
 tiles[tile_number,0] = tile_number;
 check_neighbors.thistile_number = tile_number;
 check_neighbors.thistile_type = tile_type;

 }
 
 void OnGUI()
 {
 if (Active_Layer == 1)
 {
 if (GUILayout.Button("Road",GUILayout.Height(25)))
 {
 tiles[tile_number,1] = 1;
 Active_Layer = 3;
 }
 
 if (GUILayout.Button("House",GUILayout.Height(25)))
 {
 tiles[tile_number,1] = 2;
 Active_Layer = 3;
 Instantiate(house,new Vector3(marker_x,0,marker_z),Quaternion.identity);
 }
 
 if (GUILayout.Button("Storage",GUILayout.Height(25)))
 {
 tiles[tile_number,1] = 3;
 Active_Layer = 3;
 }
 
 if (GUILayout.Button("Granary",GUILayout.Height(25)))
 {
 tiles[tile_number,1] = 4;
 Active_Layer = 3;
 }
 
 if (GUILayout.Button("Back1",GUILayout.Height(25)))
 {
 Active_Layer = 0;
 }
 }
 
 if (Active_Layer == 3)
 {
 if (tiles[tile_number,1] == 1)
 {
 GUILayout.Label("Road built",GUILayout.Height(25));
 }
 
 if (tiles[tile_number,1] == 2)
 {
 GUILayout.Label("House built",GUILayout.Height(25));
 }
 
 if (tiles[tile_number,1] == 3)
 {
 GUILayout.Label("Storage built",GUILayout.Height(25));
 }
 
 if (tiles[tile_number,1] == 4)
 {
 GUILayout.Label("Granary built",GUILayout.Height(25));
 }
 
 if (GUILayout.Button("Back2",GUILayout.Height(25)))
 {
 Active_Layer = 0; 
 }
 }
 
 GUI.Box(new Rect(Screen.width-200,25,175,25),"current Tile_number= "+tile_number);
 GUI.Box(new Rect(Screen.width-200,75,175,25),"Tile_type= "+tiles[tile_number,1]);
 
 
 }
}

Everything works great, i can place “Buildings” exactly where i point, and if i click on top of the cube, it hits the “player_building” layer and tells me that i have a building there already, but if i click at the side of the building, it only shows the Back2 button.

Why is this happening? i can track the raycasts and they should both hit the building layer right?

ill try… me and Unity answers in general arent getting along :stuck_out_tongue:

ive put my whole script up now, ive checked the raycast even further, and it only works properly if i hit the top of the cube, even from different angles

I think i have the solution myself, ill share it with you all :slight_smile:

if i hit the side of the cube, it will hit layer 3, but will hit outside the tile, where nothing is built, thats why is wont display what is built, but just create the default layer 3 “back2” button.