Screen center pointer not triggering ui onclick
Hi, So i have a world space canvas with a button and a onclick event on.
i need the clicks to come from a point in the center of the screen instead of a mouse position im new to unity but from what i can tell raycasts are the way to go (which fits with my future plans of in world object that has a laser pointer that clicks)
i have the event system on the scene and canvas layer with graphics raycaster on and is set to UI which layerMask is also set to UI.
This code is registering the clicks but never makes it into the physics.raycast block so im assuming its always evaluating to false.
Any ideas for A why this isnt working and B how i can ignore the mouse position when clicking so its exclusively the screen center
void Update()
{
if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
{
RaycastHit hit;
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
if (Physics.Raycast(ray, out hit, rayLength, layerMask))
{
IPointerClickHandler clickHandler = hit.transform.gameObject.GetComponent<IPointerClickHandler>();
if (clickHandler != null)
{
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
clickHandler.OnPointerClick(pointerEventData);
}
}
}
}
Your answer
Follow this Question
Related Questions
How to get gameobject of script with raycast? 0 Answers
Render only a fraction (say, 75%) of the screen at run time? How to? 0 Answers
Detect MouseOver/Click for UI Canvas Object 5 Answers
OnPointerEnter blocked by something 2 Answers
How to scale canvas content according to screen resolution? 1 Answer