Hello, I’m having a problem with raycasts. The raycast only detects both cubes when it’s really near the center of 2 cubes, while I want it to detect the whole collider of a cube.
Here’s the code in question:
void RaycastChecker(RaycastHit hit)
{
Vector3 origPosition = Input.mousePosition;
Vector3 firstBlockCentrePos = new Vector3(origPosition.x - 0.5f, origPosition.y, origPosition.y);
Vector3 secondBlockCentrePos = new Vector3(origPosition.x + 0.5f, origPosition.y, origPosition.y);
Ray firstBlockRay = Camera.main.ScreenPointToRay(firstBlockCentrePos);
Ray secondBlockRay = Camera.main.ScreenPointToRay(secondBlockCentrePos);
RaycastHit hitFirstBlock;
RaycastHit hitSecondBlock;
int layermask = 1 << 9;
if (Physics.Raycast(firstBlockRay, out hitFirstBlock, Mathf.Infinity, layermask) && Physics.Raycast(secondBlockRay, out hitSecondBlock, Mathf.Infinity, layermask))
{
Debug.DrawRay(hitFirstBlock.transform.position, hitFirstBlock.transform.TransformDirection(hitFirstBlock.transform.forward) * 10, Color.yellow);
Debug.DrawRay(hitSecondBlock.transform.position, hitSecondBlock.transform.TransformDirection(hitSecondBlock.transform.forward) * 10, Color.yellow);
Debug.Log("Hit 2 objects: " + hitFirstBlock.collider.gameObject.name + " and " + hitSecondBlock.collider.gameObject.name);
Debug.Log(hitFirstBlock.transform.position);
Debug.Log(hitSecondBlock.transform.position);
}
}
}

