- Home /
Making Object Clickable(Need Guidance ASAP)
So I've been learning alot of things from the forums about using Unity and coding certain aspects of the application that I'm developing.
Now, part of our application is to click on a building and make it load another screen then displaying information about that building.
I searched the forums and most answers point to Raycasting.
I've read here that it
calculates the direction of a line corresponding to a screen position for the given camera. This line, in the form of a Ray object, can then be passed to Physics.Raycast. This function searches along the line from its origin until it makes contact with an object. The object returned by Physics.Raycast (in the RaycastHit parameter) is the object at the original screen position.
I've 1) downloaded his demo code and attached it to my First Person Controller. 2) Set the target1 parameter to my test model(a small building made in google sketchup) 3) Set the target2 parameter to my terrain. 4) I've also put a box collider on my test model. 5) I didn't edit any of the code.
I click play and when I click on the Test model it prints "Hit Nothing" but when I click on the terrain,it prints "Hit Target 2".
Can someone explain to me further how raycasting works?
What I know is that it returns the position and identifies the object where you "clicked" based on the distance calculated from the direction of the line corresponding to the screen position for the given camera..
I just need to be able to grasp this concept for me to get going in the development of our project.
I need to make the model be highlighted on hover and clickable and make it load another scene once it is clicked.
Is there any other alternatives on how to achieve what I want?
Please help me. Thank you in advance!
Answer by aldonaletto · Aug 19, 2012 at 03:42 PM
Physics.Raycast checks if the logical ray passed as argument hits some collider. If so, it returns true and fills the RaycastHit structure with info about the object. The object must have a collider, or it won't work. In your case, many things may be going wrong: the collider may be too small, or badly positioned, or something else is in front of it and blocking the building collider. You can change the script to a simpler thing like below, where the name of the clicked object will be returned - this may help you to understand what's exactly going on:
function Update () { if (Input.GetMouseButton(0)) { var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit: RaycastHit; if (Physics.Raycast(ray, hit)) { print("Hit "+hit.transform.name); } else { print("Hit nothing"); } } }@Chesley suggested OnMouseDown, which may be a better solution in your case: when you must do always the same thing independently of the object clicked, use Raycast in a camera script; when you need each object to have a different reaction when clicked, use OnMouseDown in the object's script:
function OnMouseDown(){ print(name+" was clicked!"); }
This did the trick. Thank you! :D Oh and the reason that my model can't be detected as HIT when it is clicked because I used a Box Collider ins$$anonymous$$d of a $$anonymous$$esh Collider.
Can I have a follow up question?
How can you make a 3D model glow on hover of the mouse? So that players may know that that object can be clicked or it will let them know it's an essential part of the game?
I've tried this one: function On$$anonymous$$ouseOver () { renderer.material.color -= Color(0.1, 0, 0); }
but it doesn't make the model glow nor does it do anything to the model. but when I tried this script to a cube gameobject it doesn't glow but changes it's color. I've read that I need to manipulate something in the ALPHA Channel of the model?
But how exactly can I do that?
Answer by Chesley · Aug 19, 2012 at 03:06 PM
can't you use like:
function OnMouseDown() {
what ever happens when you click here
}
sorry made an awnser, i should have made it a comment like this.