Raycasts and UI
I'm using the new input system, I have a function where I raycast looking for a navmesh to move my character to, the problem is I don't want my character to move if I click something on the UI (using the standard system, not the new one). The only "solution" I found is EventSystem.current.IsPointerOverGameObject() , but the console warns that it's not ok to use it there since it would sample the past frame, it works, but I assume there is a better way to do it without console warnings, any idea? It seems a trivial and extremely common problem to me but I have not found any real solution.
public void Mover(InputAction.CallbackContext context) {
if (!context.performed) return;
Vector2 pos = playerInputActions.Player.Position.ReadValue<Vector2>();
Ray ray = Camera.main.ScreenPointToRay(pos);
RaycastHit physicsHit;
//if (EventSystem.current.IsPointerOverGameObject()) return;
if (Physics.Raycast(ray, out physicsHit)) {
NavMeshHit navmeshHit;
int walkableMask = 1 << NavMesh.GetAreaFromName("Walkable");
if (NavMesh.SamplePosition(physicsHit.point, out navmeshHit, 1.0f, walkableMask)) {
player.SetDestination(navmeshHit.position);
}
}
}
Comment