Showing menu make the cursor disappear - c#

The cursor disappears when making a single click, like so:
How to reproduce:
get the Game Jam Menu template:
https://www.assetstore.unity3d.com/en/#!/content/40465
Add another scene with an FPS controller imported from the standard
assets.
Tick the "change scenes" in the UI -> start options component.
Press escape and touch the slider, any single click will make the
cursor disappear.
It looks like this: https://imgur.com/a/hefmP
If I tick off the mouse look -> lock cursor in the FPS controller then this doesn't happen, but I also see the cursor in my FPS game.
I tried to add Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; and the opposite when pausing and unpausing but it doesn't help. Probably because the MouseLook.cs is also updating them in:
I'd rather not couple the FPSController and the pause menu and they're also in different scenes.
So how else can I unlock the cursor when entering the pause menu? Is there an event pattern that can be used here?
It also darkens the scene, but that's for another question.

It's been modified by the MouseLook script which is used in the FirstPersonController script. One solution is to modify the MouseLook script but a simple Asset update would kill that modification.
A proper solution to this problem is to disable the FirstPersonController script when you open up any menu and then enable it back when you close that menu. Also, when you disable it, manually set Cursor.lockState to None and set the cursor to visible.
Below is a simple function that will handle that. Each of your menu open and close button should be linked to the function below. When you call it, pass false to it with the open button and true to it with the close button:
void enableFPS(bool enable)
{
GameObject fpsObj = GameObject.Find("FPSController");
FirstPersonController fpsScript = fpsObj.GetComponent<FirstPersonController>();
if (enable)
{
//Enable FPS script
fpsScript.enabled = true;
}
else
{
//Disable FPS script
fpsScript.enabled = false;
//Unlock Mouse and make it visible
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}

Related

Script attached to gameObject not functioning but enabled (with checkmark in inspector) after disabling and then re-enabling again (UNITY)

So I have a JoyStick script attached to a player prefab and I disabled it while my timer is not equal to 0 and re-enable it. It's being re-enabled but my player cannot move anymore even though the script is checked on the inspector.
so the first picture shows that the joystick is checked and enabled before i click the start game button which will initiate a timer
the second picture shows that while the timer goes, the script is disabled
and the third shows that it's being enabled again but i can't seem to make my prefab move. FYI, it's moving before i disable the script.
It's because when you disable it you are causing some issues with some variables or gameObjects that probably were accessing to that script.
For example :
public int counter
If in another script you are accessing to that counter
Player.counter = n + 1;
It is giving to you an error when you disable the script and even if you re-able it, it won't work again because of the errors. The same can occur for prefab called, public functions and so on.
Instead of disabling that script you should add a bool called isTimerZero and put it in the movement section for example
if(Input.GetKeyDown("a") && isTimerZero == false)
// move the player
else
// don't move it
In this way you will just change the bool to true when timer is 0, and it won't cause problems

I have a problem with Locking the Cursor (Unity)?

The following code locks the cursor only when I left click my mouse after pressing Escape Key. Can anyone suggest me a way to overcome this problem?
public class lockCursor : MonoBehaviour
{
public bool locked = true;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (locked == false)
{
Debug.Log("locked");
Cursor.lockState = CursorLockMode.Locked;
locked = true;
}
else
{
Debug.Log("Unlocked");
Cursor.lockState = CursorLockMode.None;
locked = false;
}
}
}
}
As said in the API for Cursor.lockState
In the Editor the cursor is automatically reset when escape is pressed, or on switching applications.
Due to escape having a special role itself within the Unity Editor the GameView loses focus when pressing it.
There is not really a (trivial) way around this since it would require to intercept they keypress before the Unity Editor itself gets the chance to handle that escape press. (Which would always happen before it is passed on to the player -> your code.)
What you can do however would be simply use a different KeyCode only for the editor and escape only in a build like e.g.
#if UNITY_EDITOR
if(Input.GetKeyDown(KeyCode.E))
#else
if(Input.GetKeyDown(KeyCode.Escape))
#endif
{
...
You can replace the E with any other KeyCode you wish to use to "simulate the escape key" only within the Unity Editor itself. (Also see Platform dependent compilation)
As derHugo mentioned in his comment, your issue lies in the API for Cursor.lockState where it says:
"In the Editor the cursor is automatically reset when escape is
pressed, or on switching applications. In the Standalone Player you
have full control over the mouse cursor, but switching applications
still resets the cursor."
When you go to press Escape, it removes you from the application/Game Window. This is why it'll appear like your cursor is no longer locked. This is also why it goes into it's locked state as soon as you click back into the game. The same thing can be demonstrated by using your code, starting the game and then switching into another window on another monitor. Then move your mouse back over to the screen where the Game Window is. It'll appear as though it is no longer in a locked state... that is until you click in the game and it recognizes the cursor again.
To solve your problem, I would recommend using any key other than KeyCode.Escape. Maybe KeyCode.Space like the API shows.
Also, to further demonstrate the issue here, once you switch the code to use KeyCode.Space, press Space to lock the cursor and then try pressing Escape. Again you'll see that it appears to be unlocked until you press the Left Mouse Button in the Game Window again. This is because the Game thinks that you are no longer in the Game Window once you press Escape.
Hope this helps!

Press a in game Button to play a animation

I recently thought about adding a InGame Button to my Game. It's not a GUI or UI Button, it's a Block, added to a Wall for example.
Dummy Code:
OnTriggerEnter(c:Collider) {
if(c.gameObject.tag =="Player")
{
//Text = "E to interact!"
if(key.pressed("e")
{
//Connect the Button to a specific Block, play a Animation
}
}
}
So how do I connect a specific Block to the Button, and if I press e, Play the Animation just on the specific block? Please keep in mind that I'm new in Unity.
Thanks for helping out!
I don't know if you already managed to create your animation, so I-ll explain from scratch. When in the editor, select your Door and press ctrl+6 to open the animation window. From here you can animate your block. When you will be done creating the animation, your block object will have a new script attached to it: an animator. You can see the animator state machine in the animator window
These are two different things:
Animation: Defines a single animation (translation, rotation, change of color, ...)
Animator: defines when an animation occurs for the corresponding gameobject. An animator can have variables (for example, a bool) that define which is the next animation to be played
Any object can have an animator (your button can have one to move when it is pressed. You door can have another one to open / close)
For instance, in your button animator, you should have three states: Idle, Press, UnPress.
The state Press will contain the animation "press" with speed 1. The state UnPress will contain the animation "press with speed -1
Then, still in the animator window, You will create links between Idle and the two other states and add a trigger condition called "OnPress" (for example)
You can do the same to animate your door
In your Button code, you will then write
public Animator Door; // In the editor, give a reference to your door. It must have an Animator script for this to work
OnTriggerEnter(c:Collider) {
if(c.gameObject.tag =="Player")
{
//Text = "E to interact!"
if(key.pressed("e")
{
GetComponent<Animator>().SetTrigger("OnPress"); // The button's animator goes to "pressed" state
Door.SetTrigger("Open"); // The door's animator goes to "open" state
}
}
}
Then you could add another trigger to unpress the button
One more thing: When you say "Connect the button to the block", I feel like you misunderstood something: Your button script should be already added to the block in the editor
Look at these two links for more information on animations:
http://docs.unity3d.com/Manual/animeditor-UsingAnimationEditor.html
http://docs.unity3d.com/Manual/AnimatorWindow.html

Cursor can't move on scene switch Unity 5

I have an issue in my project at the moment, after a player finishes a level a new scene is loaded but I'm not able to move the mouse cursor. To begin with it wouldn't show either so I added the code;
Cursor.visible = true;
Now the mouse cursor is visible but I still can't move it from the center of the scene. Any help or advice would be appreciated.
You may need to reset lockState to None as well:
Cursor.lockState = CursorLockMode.None;
See Cursor.lockState documentation.
You can also do a script that will call it once on the void Start() an example that will work.
Place the script anywhere or on the canvas or create an empty Object and place this script on it.
void Start ()
{
Cursor.lockState = CursorLockMode.None; Cursor.visible = true;
}
That will work once you leave the scene and go back to it using the script above it will kick in, and Cursor will work and begin to interact with the loaded scene.
It shouldn't be that way, but it works. Cheers!
WP

Using sprite as button in Unity

I'm makimg a game in Unity using C# developed for Android. I'm testing using Unity Remote 4. I was just wondering if a sprite can be used as a button in Unity rather than the button made in Component -> UI -> Button.
I've tried this:
private void Update()
{
if (Input.touchCount > 0)
Application.LoadLevel("startScene");
}
Which does work. The button is being used to switch from the main menu to an options menu.
Ideally, I want the button to be in the same place in the options menu to be used to go back to the main menu. The problem then is that because the scenes switch instantly, the scenes switch between each other over and over I guess because the next scene gets the touch input as well.
How can I delay the time between the scenes switching, or is there a better way to use sprites as a button??
You can create a menu system which is persistent in scenes. Have a look at This Tutorial Video for guide
Usually buttons perform their function once a tap has been performed. A tap is a press and release on the button. What you are doing is performing your function on a press which is causing your buttons to be triggered continuously while you hold your finger.
Pseudo code:
If a finger presses on the sprite
If the same finger is release on the sprite
The button has been pressed.
You can use ray casting to detect touch on the sprite
private void Update()
{
if (Input.touchCount != 1) return;
var wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
var touchPos = new Vector2(wp.x, wp.y);
if (collider2D == Physics2D.OverlapPoint(touchPos))
{
// Your code
}
}
This is one way that has worked for me:
Add the sprite that you will use as the background of the button to the stage.
Add a Collider and mark it as a trigger.
Assign a script where you add any of the following functions depending on your case.
private void OnMouseDown () {
// Runs by pressing the button
}
private void OnMouseUp () {
// Runs when button is released
}

Categories