- Home /
Two different Raycast 2D are hitting at the same time when i touch one of them
I create one raycast 2D to destroy the first gameObject on click and created a second raycast 2D to destroy the second gameObject on click.
When i click on the first gameObject both gameObjects are destroyed at the same time, why does that happen and How can I make it destroy only the object I touched?
// First gameObject Script
using UnityEngine.EventSystems;
RaycastHit2D hit1Up = Physics2D.CircleCast(transform.position, 0.5f, Vector2.up * 0.35f);
RaycastHit2D hit1Bottom = Physics2D.CircleCast(transform.position, 0.5f, Vector2.down * 0.77f);
Debug.DrawRay(transform.position, Vector3.up * 0.35f, Color.green);
Debug.DrawRay(transform.position, Vector3.down * 0.77f, Color.green);
Ray firstRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
{
if (hit1Up.collider != null || hit1Bottom.collider != null)
{
if (hit1Up.collider.tag == "TagName1" || hit1Bottom.collider.tag == "TagName1")
{
Debug.Log("You touched TagName1");
destroy(this.gameObject);
}
}
}
// Second gameObject Script
using UnityEngine.EventSystems;
RaycastHit2D hit2Up = Physics2D.CircleCast(transform.position, 0.5f, Vector2.up * 0.35f);
RaycastHit2D hit2Bottom = Physics2D.CircleCast(transform.position, 0.5f, Vector2.down * 0.77f);
Debug.DrawRay(transform.position, Vector3.up * 0.35f, Color.yellow);
Debug.DrawRay(transform.position, Vector3.down * 0.77f, Color.yellow);
Ray secondRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
{
if (hit2Up.collider != null || hit2Bottom.collider != null)
{
if (hit2Up.collider.tag == "TagName2" || hit2Bottom.collider.tag == "TagName2")
{
Debug.Log("You touched TagName2");
destroy(this.gameObject);
}
}
}
Answer by DiegoSLTS · Apr 22, 2019 at 03:43 AM
You're not using the rays for anything, you're just creating them. The objects get deleted because you're doing CircleCasts at their position (which always result from a hit in both objects) and you delete them when you press the mouse button. Here's an example on how to detect clics on 2D objects using raycast: https://kylewbanks.com/blog/unity-2d-detecting-gameobject-clicks-using-raycasts
Thanks for the response but I tried the link you posted earlier today and it gave me the same result. I also need it to work when objects are moving fast. with the link you posted, after using it, I had to click the moving object multiple times because the its collider wasn't being detected while the object is moving quickly.
That doesn't sound right. Are you following the steps one by one? That script and your's have nothing in common, it's really weird that both have the same issue. You should just literally copy paste that code, check if it works, then make changes if you need them.
I copy and paste the code. Since the object I'm clicking has to be moving, the code is not working well because the object is moving faster than the object's collider attached to it. I tried two different RaycastHit2D but that doesn't work.
Your answer
Follow this Question
Related Questions
Raycasthit2d isnt working. 2 Answers
How to get co-ordinates of end point of Raycast2D ? 1 Answer
Raycast2D/Line Renderer offset on colliders based on mouse position 0 Answers
Unity 2D -Reflecting a linerenderer 2 Answers
Raycast Not Working properly 1 Answer