Unity New Input system click UI elements - unity3d

My game is a 3D shooter, and the player can shoot if left mouse button is clicked.
The problem is that I have also an UI Menu with buttons to restart the game, but I cannot click those buttons because in my action map click is equal to shoot.
I try to disable the player input and change the current action map to none, so I can press the buttons like the old input system with the Onclick() but it doesn't work.
At the time of displaying a Game Over screen I tried to disable the lock of the cursor.
PlayerController.sharedInstance.GetComponent<ThirdPersonController>().enabled = false;
PlayerController.sharedInstance.GetComponent<StarterAssetsInputs>().cursorLocked = false;
PlayerController.sharedInstance.GetComponent<StarterAssetsInputs>().cursorInputForLook = true;
I think I can start shooting raycast and check what element of the canvas is hit, and by the name of the button perform actions by I don't think this is the best idea.
Could you tell how can I click my UI elements please?
Thank you in advance.

Related

Unity - TextMeshPro button is clicked through

I am making a Tower Defence game for mobile and I'm struggling to fix this bug for the last couple of weeks. In order to build a tower, I tap on a tile, which pops up a UI menu with a couple of towers to choose from. If behind the location I tap on the UI to choose the tower there is another valid position to build a tower, the tap goes through the UI and selects the new valid tile.
I have an empty game object, which nests a canvas, which nests the buttons.
I have tried using the following possible fixes, but didn't work in my case:
Graphic raycaster on the canvas
if (EventSystem.current.IsPointerOverGameObject())
return;
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
return;
How should I proceed?
You could try putting a sprite behind the UI object when it gets opened so that the tap only applies to the button and the sprite. You could also make the sprite transparent.

Why is my 2nd mouse button not detecting unity (beginner)

I am trying to make a multiplayer fps game using photon and the ads is a big part. For some reason, the right mouse button is not registering, but every other one is. It works on my mouse outside of unity, but it does not work inside. My lmb is working for the ads, so I do not know why this is the case.
Here is the snippet for the aim;
}
if (Input.GetMouseButton(2))
{
animator.SetBool("rmb", true);
}
else
{
animator.SetBool("rmb", false);
}
GetMouseButton(2) is for the middle-click button (pushing down on the scroll wheel), not the right mouse button. Use GetMouseButton(1) instead:
Small tip, you can skip the if check entirely just by passing the result of Input.GetMouseButton directly to the "rmb" parameter of the animator:
animator.SetBool("rmb", Input.GetMouseButton(1));
Documentation: Scripting API: Input.GetMouseButton

Start game if anything but a UI Button is touched

I'm building an iOS game and I had the game set up to start whenever the user first touched the screen. This was working out perfectly fine for testing until I added menu buttons as an overlay that would disappear when the game starts. Obviously I can no longer just check for user input, because the game now starts even if I tap a UI button.
I tried adding an invisible UI panel with a box collider behind my UI buttons with an OnPointerDown that would start the game, but for some reason it was acting very inconsistent and would sometime require 5+ taps to start the game. It also wouldn't start if you tapped on the player because it would hit his collider instead.
Is there a better way to do this?

Allowing click or raycast to only certain items at time

This is kind of hard to explain so I try to do my best using the following image.
So the case is this: player has selected drill from inventory and "Confirmation menu" (A, S & D) has popped up. Now the player must select one of these actions (A = accept position, S = put item back to inventory, D = keep moving object around). Before the player chooses from one of these action he should not be able to do NOTHING else.
The problem is that if the player clicks the minimap in the upper right corner a button click is detected and menu is opened. If the player clicks the car (detected with Raycast) player gets inside. If the player drags the joysticks he starts walking around (using Unity's Joystick script). NONE of these should be allowed.
To the question: does Unity have some kind of method to "Prevent any kind of click detection to any other objects but these" OR "If mouse is not over this object do nothing when clicked"? There probably is nothing like that so what would be the best workaround?
Because these "click to do something" actions are triggered from various places with various styles (buttons and raycasts in tens of different scripts) I try to avoid doing this by setting booleans everywhere. Open ideas are more than welcome.
An easy workaround could be via using GUI Depth (https://docs.unity3d.com/ScriptReference/GUI-depth.html).
If "Confirmation menu" guiDepth is on top and all the other GUIs are below, you could add another GUI in between covering the whole screen; a Raycast would then return the first element and if it's the middle GUI just do nothing.

simple UI menu and canvas for DK2

I am using Unity 5.1.2p3 with DK2 SDK 0.6.0.1 and I understand from this post that Screen Space - Overlay is not supported in Unity VR. It is recommended to use Screen Space - Camera (which in my case does not work) or World Space (which I am using now) but I need someone to help me understand how I get simple menu with buttons and toggles to show as a still image and how I can make selections and button presses with my mouse cursor.
I have created a menu for my app, with 4 toggles and 1 button. When I check the Virtual Reality Supported option with the Oculus being in Direct Mode and Canvas being in World Space, I can see it in VR, but I cannot see/find my mouse cursor to tick one of the toggles.
When I take off the headset, on my monitor's Game View tab, I can see and even use the mouse and select a toggle. Obviously, I have to keep the headset steady, so in my Game View, things do not shake!
Another thing I notice is that the VR camera is the same as the Main Camera in the Unity Hierarchy, but when I take off the headset and move it around, the position of the camera does not change, only looking up and down and around is reflected.
How do I simply do a static menu like a 2D surface that does not move in VR and a user can use button presses and muse clicks with the headset on? What settings are required for this way of doing UI and canvas stuff? There are 2 attachments, showing my current settings...
Are you specifically wanting to use the mouse? If you look through a blog entry I wrote below, it will show you how to use Gaze looking to trigger menu buttons:
http://talesfromtherift.com/vr-gaze-input/
You can achieve this by some code I list there that raycasts from the center of the screen and if it hits any UI, it will trigger the correct events and make it clickable by button (or by time).