So I’m, building a game that involves clicking on Objects in an Isometric 3D scene, and have been having issues with raycasts from clicks not registering correctly. Upon investigating, it seems the Isometric Camera is miscalculating the collision of every object in the scene.
Eg. I placed a Cube below with standard collision, and turning Editor Gizmos on in game view clearly shows the cube offset from it’s collision, which you can see is correct in Scene View.
It seems to get worse the further from center screen the object is, so I’m aware it’s going to be an Isometric camera issue. My question is am I able to counteract this somehow?
How is the 3D geometry being rendered in the game view? Something tells me that the camera is in orthographic mode. If so, that would likely explain the visual offset since the collider is drawn with perspective but the wireframe cube isn’t.
I’d say that most likely your raycast code has a problem, please post that. If you use ScreenToWorldPoint then switch to ScreenPointToRay or pass the camera’s nearClipPlane as the z value. If you use z=0 in orthographic mode that would cause the disconnect you observe.
Sorry, yes, when I said Isometric Camera, I meant the camera is in Orthographic mode.
That’s what I meant by the last line, I’m aware the issue is one of Raycasts not properly handling the Ortographic camera properly.
I used Debug.DrawLine to draw my clicks and they match with the perspective view (IE. they hit only when I click the Gizmo for the Colliders position, not the actual on screen object)
(The Left 3 Clicks go to the left and Miss, while the Right 3 Connect, despite visually clicking off the onscreen cube)
I’ve tried both ScreenPointToRay and ScreenToWorldPoint, both seem to mimic the behaviour above.
Here’s my current code for doing the testing above, hopefully it’s just something simple:
(The Glitch Component check is just how I know I’ve clicked a valid object as opposed to the Wall)
void Update()
{
if(Input.GetMouseButtonDown(0) && !IsoCanvas.canvas.selected)
{
RaycastHit hit;
Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);;
Physics.Raycast(ray,out hit);
if(hit.transform)
{
GlitchObject glitch = hit.collider.GetComponent<GlitchObject>();
if(glitch && !glitch.moving)
{
Debug.DrawLine(ray.origin,hit.point,Color.green,200f);
}
else
{
Debug.DrawLine(ray.origin,hit.point,Color.red,200f);
}
}
}
}
This is … slightly unusual. Normally you would use Camera.main. Is perhaps the script on an object with a separate camera component which might not be set to orthographic? Even if it is, it would have to have the exact same settings as the main camera!
Is the Camera perhaps flagged to be a “physical” camera? I’m not sure if an orthographic camera has this setting, but if it has and it’s set it would explain the discrepancy.