- Home /
Why is this raycast still hitting ground below a UI button, even after I pass a check to see if finger is above button (as per Unity tutorial)
Hi there all. I've been reading lots and trying many variations of this method to fix my issue, but cannot quite seem to understand it all. The "...IsPointerOverGameObject(t.fingerid)" part is supposed to stop the touch being counted in my update method (so that it doesnt call the related functions in my 'Goodguys' array.)
The actual result i want is that a touch just to the screen will set the destination of the players NavMesh, then there is a UI button with Events Trigger on it, if the player holds that button in, then also presses somewhere on the screen, that will make the Goodguys shoot their guns.
It all works fine if you dont press the Shoot button. But if you do, the debug.log still states that "object touched is Ground" and the players (/or Goodguys) still just turn around, they will shoot, so know the method is having some effect, but their target underneath the UI button, and AFAIK my code should be stopping that from happening already.
Anyone kind enough so spot what I have done wrong?? Many thanks :)
public class TouchHandler : MonoBehaviour { RaycastHit hit; GameObject objectBeingTouched;
Camera cam;
GameObject[] goodguys;
private void Awake()
{
cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
}
void Update()
{
if (Input.touchCount > 0)
{
goodguys = GameObject.FindGameObjectsWithTag("Goodguy");
foreach (Touch t in Input.touches)
{
Ray ray = cam.ScreenPointToRay(t.position);
if (Physics.Raycast(ray, out hit))
{
objectBeingTouched = hit.transform.gameObject;
if (t.phase == TouchPhase.Began)
{
if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(t.fingerId))
{
foreach (GameObject goodguy in goodguys)
{
goodguy.SendMessage("OnTouchStart", hit, SendMessageOptions.DontRequireReceiver);
}
}
}
if (t.phase == TouchPhase.Moved)
{
if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(t.fingerId))
{
foreach (GameObject goodguy in goodguys)
{
goodguy.SendMessage("OnTouchMoved", hit, SendMessageOptions.DontRequireReceiver);
}
}
}
if (t.phase == TouchPhase.Stationary)
{
if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(t.fingerId))
{
foreach (GameObject goodguy in goodguys)
{
goodguy.SendMessage("OnTouchStill", hit, SendMessageOptions.DontRequireReceiver);
}
}
}
if (t.phase == TouchPhase.Ended)
{
if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(t.fingerId))
{
foreach (GameObject goodguy in goodguys)
{
goodguy.SendMessage("OnTouchEnded", hit, SendMessageOptions.DontRequireReceiver);
}
}
}
if (t.phase == TouchPhase.Canceled)
{
if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(t.fingerId))
{
foreach (GameObject goodguy in goodguys)
{
goodguy.SendMessage("OnTouchCancelled", hit, SendMessageOptions.DontRequireReceiver);
}
}
}
}
}
}
}
// UI button toggles:
public void UiButtonShootModeToggle()
{
if (goodguys != null)
{
foreach (GameObject goodguy in goodguys)
{
Goodguy gg = goodguy.GetComponent<Goodguy>();
if (gg.currentControlMode != Goodguy.ControlMode.SHOOT)
{
gg.currentControlMode = Goodguy.ControlMode.SHOOT;
}
else
{
gg.currentControlMode = Goodguy.ControlMode.MOVE;
}
}
}
}
}
Answer by waizui · Dec 12, 2018 at 12:22 PM
same issue ,how did you fixed it?
hi sorry i don't remember. But I know I haven't run into any more problems like it with other projects since then. I think I do the raycast slightly differently now with less lines of code. Something like if (Physics3d.Raycast(ray, out hit, rayLength){ return hit.collider.gameObject} then the hit tells me what object the ray hit first (i think!).
Hope that helps lol :S
Your answer
Follow this Question
Related Questions
EventSystem.current.IsPointerOverGameObject(t.fingerId) - Not working on Android builds - Unity 2 Answers
Android touch input Null Reference exception error 0 Answers
The bullet doesn't move correctly 1 Answer
Unity Android Change Color of Navigation bar 0 Answers
UI Touch Events not working? 1 Answer