Question by
mhmmdemen · Dec 22, 2020 at 02:30 PM ·
raycasttouchraycasthitraycasthit2dtouch screen
RaycastHit2D problem
I just click on the blue gameObject once. But other gameObjects are also clicked.
When I draw to see Ray, I only see one ray. Why are other gameObjects clicked as if Ray were sent?
I also log clicks and 14 logs were sent for one click.
When I use OnMouseDown() its working suceessfully. But I gonna make it for mobile phones.
How can I solve this problem? My codes are as follows.
void Update()
{
if (Input.GetMouseButtonDown(0) && !isClicked)
{
isClicked = true;
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(pos, Camera.main.transform.position);
Debug.DrawRay(pos, Camera.main.transform.position, Color.red, 2f);
Debug.Log("Ray Worked!");
if (hit.transform != null && hit.collider != null && hit.collider.tag == "Virus")
{
splatVirus();
}
}
if (Input.GetMouseButtonUp(0) && isClicked)
{
isClicked = false;
}
}
ekran-goruntusu-2020-12-22-165125.png
(123.2 kB)
Comment