- Home /
Question by
prakyath_unity · Nov 27, 2020 at 05:52 AM ·
unity 2dprefabsdetectiontouch controls
Is there any way to check if two prefabs are touched at the same time
I have two prefabs spawned at the different position, both are having same script . Now I need to destroy the prefabs if they are touched at the same time. Is there any easiest way to do this? Note: both the prefabs are having collider2D attached to it. And also multi touch is enabled. Thanks!
Comment
Best Answer
Answer by prakyathd801 · Dec 31, 2020 at 01:16 PM
This is the code which enabled multitouch
if (Input.touchCount > 0)
{
foreach (Touch t in Input.touches)
{
Debug.LogError("current touch "+Input.touches);
Debug.LogError("touch count is "+Input.touchCount);
Vector3 touchPos = t.position;
if(t.phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(touchPos);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
if (hit.collider != null && hit.transform == transform)
{
Debug.Log("Hit: " + hit.transform.name);
myOnMouseDown(); //This function will be resposible for destroying the prefab
break;
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613020626im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to spawn multiple prefabs - Tilemap 0 Answers
How to instantiate a player's weapon after picking it up? 1 Answer
How to convert mouse input to mobile touch in unity 1 Answer
How to call array element that has bool value true 1 Answer
How can i reset positions of Prefab children's in unity 2d. children's are also prefabs 0 Answers