- Home /
Raycasting to make Buildings Clickable
I have a problem.
I used this code which I found somewhere here in the forums..it's a raycasting method for clicking objects.
` script ExecuteInEditMode
var gSkin : GUISkin;
var backdrop : Texture2D;
var target1: Transform;
var target2: Transform;
private var isLoading = false;
function OnGUI()
{
if (gSkin)
GUI.skin = gSkin;
else
Debug.Log("StartMenuGUI: GUI Skin object missing!");
var backgroundStyle : GUIStyle = new GUIStyle();
backgroundStyle.normal.background = backdrop;
GUI.Label(Rect((Screen.width - (Screen.height *2)) *0.75,0,Screen.height *2,
Screen.height ),"",backgroundStyle);
//Loading Screen
if (isLoading)
Application .LoadLevel("Rotating Arch");
}
function Update () {
if (Input.GetMouseButton(0)) {
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)) {
if (hit.transform == target1) {
print("Hit target 1");
isLoading = true;
} else if (hit.transform == target2) {
print("Hit target 2");
}
} else {
print("Hit nothing");
}
}
}`
Anyway..
This works fine for those buildings that have single meshes. But sometimes when the model has multiple meshes...I choose the one that covers all parts of the model(that's where I put my collider) then test if it will take a "hit".
Apparently...it does not work.
NOTE: in the code sample there are 2 targets only. Because I test the buildings 1 by 1 first to know if I put the colliders in the right mesh. But in the long run...once I have all colliders in the right place I will have multiple targets.
The goal is to make each building "clickable" so that when it is "hit" it will go to a scene where it's details are shown.
The problem is that Multiple Meshed models seem to not work for this code. :( I need to fix this asap for this is part of my thesis XD
Did I do something wrong? Is there any other alternative for this kind of thing?
ANOTHER NOTE: I attach the above script to an FPS Controller. This is part of a "Free Roam" function of my thesis. More of a digital tour of a place :)
Have you ticked convex for the complex mesh collider?
Also, use a
Debug.DrawLine(other.ScreenPointToRay(Input.mousePosition),hit.point,Color.white);
to check if the ray passes through or actually collides with the obejct.
sorry for the late reply. anyway will try all of your suggestions and get back to you as soon as I can. Thanks! :D
Answer by Fattie · Aug 30, 2012 at 06:13 AM
are you familiar with the physics "layers" system in Unity3D ?
in may situations like this you'd simply make a set of separate colliders (likely simple boxes) for the use of the "click on a building" system.
I've tried box colliders on each models...it won't work. I don't know why :(
Sorry for the super late reply! Anyway I've managed to make the box collider work with the code. The problem was(I Think) is that the models have a mesh collider then I put over a box collider. I think it messes up the Raycast or something.
So I just removed the mesh colliders and then put the box colliders in the array of targets.
Thanks again for the help.
Your answer
Follow this Question
Related Questions
Ray cast affects all colliders in scene if mouse button is not releasesd. 1 Answer
raycast screenpointtoray isnt working for some reason 0 Answers
How can I let the ray to the edge of the capsule? 1 Answer
Raycast not hitting the collider properly it always has a weird offset 0 Answers
RTS Object selection Ignoring Colliders 2 Answers