I'm currently using a Raycast both for my movement check aswell as a focus-setting check, both checks use pretty much the same code. The problem i'm having is that the Raycast goes through some objects and targets the background no matter what i've tried to far. As you can see in this screenshot, I first clicked on the black sphere (Just a 3D Sphere in my 2D world with a default Sphere Collider) and the Log shows so aswell, however when i click on the white box which the second Debug Log is supposed to be, it just ignores the box collider and goes straight to the Background (the green sprite). The same problem with the Interactable check, code works completely fine with the Sphere but not with the box. Here's the Code i currently use:
//Movement
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
Debug.Log("We hit " + hit.collider.name + " " + hit.point);
RemoveFocus();
targetPosition = hit.point;
}
}
if(targetPosition != (Vector2)transform.position)
transform.position = Vector3.MoveTowards(transform.position, targetPosition, maxSpeed * Time.deltaTime);
//Check for Interactable, pretty much same code...
if (Input.GetKeyDown(KeyCode.Mouse1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 200))
{
Interactable interactable = hit.collider.GetComponent<Interactable>();
if(interactable != null)
{
SetFocus(interactable);
}
}
}