Raycast2D Two Objects at the same time
I am trying to execute a function only when two objects, one of type "touchArea" and other of type "Tiles" . For that purpose, I did this
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = 10;
Vector3 screenPos = GameCamera.ScreenToWorldPoint(mousePos);
RaycastHit2D hit = Physics2D.Raycast(screenPos, Vector2.zero);
if (hit)
{
if(hit.collider.tag=="touchArea" && hit.collider.tag == "tiles")
{
print(hit.collider.name);
}
}
}
It works fine when only looking for one type, but when looking for two that are overlapping, it doesnt work. One of them is a trigger type. Thanks in advance.
Answer by oligranaat · Dec 06, 2016 at 09:48 AM
You could use a if-else statement. Try if that works.
Answer by doraemoe · Apr 01, 2017 at 01:40 PM
I'm having the same problem. The solution is simple, just use Physics2D.RaycastAll
instead of Physics2D.Raycast
. It will return an array of hit objects.
Your answer
Follow this Question
Related Questions
Tag doesn't work when gameObject is a child of the Main camera? 1 Answer
How to avoid overlapping of GameObject without rigidbody? 1 Answer
reaching the limit of a Raycast[] array? 0 Answers
One 2D Raycast Not Stopping Even Though Others Do? 0 Answers
GameObject overlapping on random spawn 0 Answers