Question by
Knightly-Valor · Feb 19, 2019 at 03:49 AM ·
2draycastcollider
Raycast not detecting?
void Update()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hitInfo = Physics2D.Raycast(ray.origin, ray.direction);
if(hitInfo.collider != null && hitInfo.collider.CompareTag("Character"))
{
Debug.LogFormat("mouse clicked on {0}", hitInfo.collider.name);
}
}
}
I'm trying to get the following: ->Input a click ->If I click something tagged character, the debug shows what character's name I'm clicking on.
I have a prefab with a tag labeled character. I've placed him into the worldspace in 2d and clicked on him. It's not detecting the click.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612191959im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
CircleCast hit its origin... 0 Answers
Issue with 2D raycast 0 Answers
Touch Collider 0 Answers
Problem with raycasting towards mouse 0 Answers
Jitter with Tilemap Collider's but no Jitter on standard Box Collider 1 Answer