Case:
I have a UI (a part of the canvas) with buttons and sliders. In my scene there are also cameras with mouse pan by left mouse button.
Problem:
When I use a slider the camera also pans.
Expected:
Don’t pan the camera when I use a UI slider. Don’t process Input.GetMouseButtonDown on other scripts, when I use the UI. Similar at html / javascript event.preventDefault().
It is an older question but sure will someone appreciate an answer…
One of possible solutions is to make raycast test of a ui item.
Modify your camera-panning method like this:
if ( Input.GetMouseButtonDown( 0 ) ) {
GraphicRaycaster gr = FindObjectOfType<GraphicRaycaster>();
PointerEventData ped = new PointerEventData(null);
ped.position = Input.mousePosition;
List<RaycastResult> results = new List<RaycastResult>();
gr.Raycast(ped, results);
if (results.Count == 0) {
PerformCameraPanning();
}
}
This way camera pans only in case no UI item was found in the way of the mouse current position.
Ps: I believe GraphicRaycaster component is added automatically to the UI root GameObject as soon as an Image component is added.
For Unity 2019, use EventSystem.current.IsPointerOverGameObject().
Yeah, I did everything. It prints collision with Tag and bool constantly, yeah. When I press E, It prints my own "car is ready to go" (if script is enabled) and "isCar is true" (if this boolean changes). So it means, that this script on a car enables everything for one frame and instantly disables it. I can see the player character twitch as I press the button.. The worst thing is that I've made scripts with similar actions before and everything worked.
Hey @Domvel
Jason Weimann on YouTube has a great solution
you’ll need line 2 and lines 8 to 11 from the script he shows
Did you change to GetKeyDownin your exit method? Because if using up, the code might still see that it's up, and continue with your next method of exit car. As in it basically is going right to exit car within the same frame. You could do your own simple bool check on your inputs, that way once the getInCar method reads, it immediately turns off KeyWasPressed, basically
Yeah, I did everything. It prints collision with Tag and bool constantly, yeah. When I press E, It prints my own "car is ready to go" (if script is enabled) and "isCar is true" (if this boolean changes). So it means, that this script on a car enables everything for one frame and instantly disables it. I can see the player character twitch as I press the button.. The worst thing is that I've made scripts with similar actions before and everything worked.
– ivanscreamIs it possible to contact you somewhere else? It may be easier to explain the problem fully with quick updates.. would help me a lot
– ivanscream