- Home /
Raycast only hits the layer when hitting top of cube
hello there :)
i currently have 2 layermask, Ground and Building. Ground layer is the green area, Building layer is the cube. The red shows the raycasts.
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?
Could you try to format the code better? It's very difficult to read in it's current form.
Answer by Gravn · Jul 18, 2012 at 10:39 PM
I think i have the solution myself, ill share it with you all :)
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.
Answer by Gravn · Jul 18, 2012 at 04:31 PM
ill try.. me and Unity answers in general arent getting along :P
Answer by Gravn · Jul 18, 2012 at 04:36 PM
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
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Physics.Raycast to Physics2D.Raycast? 1 Answer
C# raycast shooting script broken 1 Answer
Raycasting in 2D is not working 0 Answers