- Home /
Getting no response from Raycast
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!");
}
}
}
Do the things you try to click have a collier attached? Have you tried to debug the ray?
I'm fairly new to this so i don't know entirely what you mean, but the thing i want to click is a simple cube with no addons. I also want to mention that i don't get any printout: No "Click!", No "Hit!". How do i debug the ray?
The code works. Are you trying to press on the cube in the scene-view, ins$$anonymous$$d of the game-view? Another thing I've seen people new to Unity do is forget to press Play before doing tests like these.
Answer by phxvyper · Jun 03, 2013 at 08:12 AM
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!".
Answer by rapowke · Jun 03, 2013 at 10:28 AM
Neither of it is seems to be the problem. Do i need to add to a component?
@rapowke sorry for not responding. Anyways, when testing your game, you are pressing the play button, right? And then when you change something you're stopping the game then playing it again? Also, did you try out the code i suggested?
Yeah I do. I think i found the answer. I added it to a component and it seems to work now.
@rapowke Hey! If my answer solved your issue, please accept it as the final answer by clicking on the checkmark to the left of my answer.