- Home /
IsPointerOverGameObject not working with TouchPhase.Ended
So I have a script that spawns a bullet and shoots using touch controls. It uses TouchPhase.Began, TouchPhase.Moved, and TouchPhase.Ended. The !EventSystem.current.IsPointerOverGameObject(touch.fingerId) works properly on the first two but not on the last one. The issue with this is that the last one instantiates the bullet so you can still shoot when clicking on UI elements. I can confirm its isolated to just the TouchPhase.Ended portion because the behavior nested in the first two won't work over the UI elements. Has anyone ran into this and found a solution?
touch = Input.GetTouch(0);
if (!EventSystem.current.IsPointerOverGameObject(touch.fingerId))
{
if (touch.phase == TouchPhase.Began)
{
dragStartPos = Camera.main.ScreenToWorldPoint(touch.position);
dragStartPos.z = 0f;
direction = dragStartPos - transform.position;
transform.right = direction;
}
if (touch.phase == TouchPhase.Moved)
{
float width = lr2.startWidth;
lr2.material.mainTextureScale = new Vector2(1f / width, 1.0f);
Vector3 draggingPos = Camera.main.ScreenToWorldPoint(touch.position);
draggingPos.z = 0f;
direction = draggingPos - transform.position;
transform.right = direction;
Vector2 force = transform.right * power;
Vector2[] trajectory = Plot(rb, shotPoint.position, force, 500);
lr2.positionCount = trajectory.Length;
Vector3[] positions = new Vector3[trajectory.Length];
for (int i = 0; i < trajectory.Length; i++)
{
positions[i] = trajectory[i];
}
lr2.SetPositions(positions);
}
if (touch.phase == TouchPhase.Ended)
{
lr2.positionCount = 0;
GameObject newBullet = Instantiate(bullet, shotPoint.position, shotPoint.rotation);
Vector3 force = transform.right * power;
newBullet.GetComponent<Rigidbody2D>().velocity = force;
}
}
Your answer
Follow this Question
Related Questions
Unity UI - layout fixed number of buttons 1 Answer
Problem with Main Menu 1 Answer
Make an obect take priority over another when touched? 0 Answers
is it Possible to use navigation on non UI elements? 1 Answer
how to make a shop system 1 Answer