Hi, i’m trying to do a simple point click code, but strangely, nothing happens at all. Here is my code, what am i doing wrong?
function Update ()
{
if(Input.GetMouseButtonDown(0))
{
print("Click!");
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100.0))
{
print("Hit!");
}
}
}
Firstly, the print function is used for Debug. So, unless you’re trying to log to the Debug Console/Log File, you’re using the wrong function.
If you wish to notify after a click/hit, then create a string and associated it in the parameters of a GUI TextField or TextArea…
var action = "";
OnGUI() {
GUI.TextField(0, 0, new Rect(0, 0, 100, 20), action);
}
Update() {
if(Input.GetMouseButtonDown(0)) {
action = "Click!";
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, 100.0)) {
action = "Hit!";
}
}
}
This is display a TextField on the top left of the screen, whenever the LeftMouseButton is clicked, it will display “Click!”. If, after the click, the Raycast hits the cube, then it will display “Hit!”.
Neither of it is seems to be the problem. Do i need to add to a component?