- Home /
Raycast forward
I am having trouble using Physics raycast. When I click the Input.mousePosition, the wrong ball is select. The bottom left ball is 0, 0, but when I click it ball 0, 3 gets selected (show with yellow background instead of red).
It is also inconsistent such that when I restart the game mode, I get 0, 1 when I click again. I have tried using ScreenToWorldPoint() instead and using the overload method with direction. Am I doing anything wrong here?

#if UNITY_EDITOR
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
#else
ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
#endif
if(Physics.Raycast(ray, out hitInfo, 51f)){
if(hitInfo.collider.tag == "Tile"){
selectTile = hitInfo.collider.name.Split(',');
logic.checkTile(int.Parse(selectTile[1]), int.Parse(selectTile[2]));
Debug.Log(selectTile[1] + ", " + selectTile[2]);
}
}
I don't see any issues with the code. Verify a couple of things. You can use Debug.DrawRay() to draw the ray and verify the position. You could also verify your na$$anonymous$$g scheme by hiding the tile you click on.
hitInfo.collider.renderer.enabled = false;
If the ray is going to the right place and the na$$anonymous$$g scheme is correct, check the size of your colliders. $$anonymous$$ake sure the collider size matches the tile size. If you have an oversized collider then you might get this behavior.
Your answer
Follow this Question
Related Questions
raycast rigidbody addforce 2 Answers
DontGoThroughThings vs Triggers 1 Answer
How can I make a grappling hook with RayCast? 1 Answer
Unity 5 - Hex Tile change color on mouseover 0 Answers
RaycastHit.Normal for Meshes? 0 Answers