- Home /
Problem is not reproducible or outdated
ScreenPointToRay not responding
I'm trying to create a simple RTS, and currently I'm trying to nail down the basic functionality of pointing at a unit and selecting it. My intended solution involves raycasting, specifically ScreenPointToRay.
void Update ()
{
print(Input.mousePosition);
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;
Ray ray = gameCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
print(hit.transform.position);
}
}
if (Input.GetMouseButtonDown(2))
{
if (unitSelected == true)
{
}
}
}
I was trying to test my code by having the script return the world space position that it had hit, but for some reason it never returns a value. I tried the raycasting function without the if check, so it ran every Update, and that seemed to return some world space coordinates, but very infrequently.
I'm very confused as to what's going wrong here to cause such odd behaviour, and I can't find an answer anywhere else.
Uhm you know that you're actually checking the right mouse button for the raycast and the middle mouse button in your second if statement? $$anonymous$$aybe that's already your problem? Indices start at "o":
0 -> left mouse button
1 -> right mouse button
2 -> middle mouse button
Oh. Now it works. Herp a derp I'm stupid.
I think I was confusing mouse button "1" with "Fire1". I'll have to remember they're not the same for next time,