Question by
tally_er · Mar 05, 2017 at 03:32 PM ·
c#2d-physicsclicking
Clicking a specific object using boxcollider2d and raycast2d
I am trying to set up a 2D map where clicking on specific items brings up a response. I have written the below code and cannot understand why nothing is happening. Please help out this newbie.
public class InputManager : MonoBehaviour
{
public GameObject home;
public int layerMask = 1 << 9;
public void Update()
{
bool click = Input.GetMouseButtonDown(0);
if (click)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D[] hits = Physics2D.GetRayIntersectionAll(ray, Mathf.Infinity, layerMask);
foreach (var hit in hits)
{
Debug.Log(hit.transform);
Debug.Log(hit.collider);
if (hit.collider == home)
{
Debug.Log("clicked on home!");
}
}
}
}
Thanks
Comment
Your answer
Follow this Question
Related Questions
I need help with AI,Force not working 0 Answers
How do I make a ball bounce in a circle? 0 Answers
How to align 2d object to terrain? 2 Answers