How Can I Detect Objets on Click with Raycast?
Hello everyone,
I have a lot of problem to use raycast. I need it to detect multiple objects (collider, transform, tag..) with mouse click in 2D. My code:
Camera cam;
void Start()
{
cam = GetComponent<Camera>();
}
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit2D[] hits = Physics2D.GetRayIntersectionAll(ray, Mathf.Infinity);
Debug.Log("clickOk");
foreach (RaycastHit2D hit in hits)
{
if (hit.collider.tag == "case")
{
Debug.Log("raycastOk");
}
}
}
}
This script is attached on my panel object, the panel is the parent of all the objects i need to detect. The "clickOk" is printed but not the "raycastOk". More informations:
Can you help me please?
Thank you in advance.
capture-decran-2020-06-21-a-205840.png
(177.2 kB)
Comment
Your answer
Follow this Question
Related Questions
Raycast from camera only working from certain angles 1 Answer
Raycast hitting air 0 Answers
Raycast from Camera 0 Answers
How to add score when destroy another player with Raycast? 2 Answers