- Home /
C# Raycast isn't working?
For some reason my raycast isn't working. I was wondering if someone could help me. Here is my code:
void Update () {
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Ray ray = Camera.main.ScreenPointToRay(mousePos);
if (Physics.Raycast(ray, out hit, Mathf.Infinity, 0)) {
Debug.Log("Works");
}
}
}
For some reason it isn't logging the message.
Comment
Answer by supernat · Jun 09, 2014 at 05:06 PM
You are converting the mouse screen point to world coordinates, then passing the world coordinates into ScreenPointToRay which requires a screen point. So that won't work. Just pass in Input.mousePosition instead.
Your answer
![](https://koobas.hobune.stream/wayback/20220613151156im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Player lives script help 1 Answer
Trigger GUI with a key when colliding (C#)? 1 Answer
Call StartCoroutine() from inside a function other than Start!? 2 Answers
Pause Game When Function Is Enabled? 3 Answers