snoe
snoe

Reputation: 11

ScreenPointToRay always going to (0, 0, 0)

I'm doing an incredibly simple raycast, copied almost directly from the docs themselves, but the raycast hit always goes to the world origin (0, 0, 0). The only caveat is that I'm using the new(ish) input system, but as far as I can tell that shouldn't make a difference.

My code is dead simple:

Vector3 mousePos = Mouse.current.position.ReadValue(); // Debug shows an appropriate value being returned from input system

RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(mousePos);

if (Physics.Raycast(ray, out hit)) {
    Debug.DrawLine(mainCamera.transform.position, hit.transform.position); // Always goes from Camera to 0, 0, 0
}

It's honestly so simple I'm not even sure where else to look, and it's driving me crazy. Anyone have any thoughts?

Upvotes: 1

Views: 805

Answers (1)

cmdexecutor
cmdexecutor

Reputation: 329

Just checked your code in 2019.4.1f1 with Input System package v 1.0.0 and it seems correct. Only have replaced your

Debug.DrawLine(mainCamera.transform.position, hit.transform.position);

with

Debug.DrawLine(Camera.main.transform.position, hit.transform.position);

Try to check if you have a camera with "MainCamera" tag in the scene.

Raycasting

Upvotes: 1

Related Questions