Pause don't works (Unity c#) - c#

I writing runner game on Unity (C#) for mobile phones.
I made Pause button on screen, using Canvas - Button.
Also I made code for Pause in Platformer2DUserControl script.
Here it is code of this script:
using UnityEngine;
using UnitySampleAssets.CrossPlatformInput;
namespace UnitySampleAssets._2D
{
[RequireComponent(typeof(PlatformerCharacter2D))]
public class Platformer2DUserControl : MonoBehaviour
{
private PlatformerCharacter2D character;
private bool jump;
public bool paused;
private void Awake()
{
character = GetComponent<PlatformerCharacter2D>();
paused = false;
}
private void Update()
{
/*if (Input.GetButton("Fire1"))
{
}*/
if (!jump)
// Read the jump input in Update so button presses aren't missed.
jump = Input.GetButton("Fire1"); //&& CrossPlatformInputManager.GetButtonDown("Jump");
}
private void FixedUpdate()
{
// Read the inputs.
bool crouch = Input.GetKey(KeyCode.LeftControl);
// float h = CrossPlatformInputManager.GetAxis("Horizontal");
// Pass all parameters to the character control script.
character.Move(1, false, jump);
jump = false;
}
public void Pause()
{
if (!jump)
// Read the jump input in Update so button presses aren't missed.
jump = Input.GetButton("Fire1"); //&& CrossPlatformInputManager.GetButtonDown("Jump");
paused = !paused;
if (paused)
{
jump = !jump;
Time.timeScale = 0;
}
else if (!paused)
{
// jump = Input.GetButton("Fire1");
Time.timeScale = 1;
}
}
}
}
My Pause Button is WORKS well. But when I tap it , my character is jumping and game is pausing.
I want to make that , when I tapping the button game just pausing and character don't jump.
How I can make it. Thank's for help.

I would advise you not to use the same input for jumping and pausing. Also, separate your jump and pause functionalities into separate functions. For pausing, create a UI button on the screen and make it call a public function on a Pause script, that will toggle pause. Then, in the same function, check if you are paused or not and adjust Time.timescale accordingly
You will have to attach the script with pause functionality on to an object that will always be in a screen (Say, a panel in your canvas or your MainCamera). Under the button, add a new onClick() function after dragging the GO with the apt script to the box. Then, select the public function aforementioned.
private bool paused = false;
//The function called by the button OnClick()
public void TogglePause()
{
paused = !paused;
if(paused)
Time.timescale = 0f;
else
Time.timescale = 1f;
}
Hope this helped!

Well, your code is messed up, but just remove the jump script from the pause method. So it will just pause...
public void Pause()
{
paused = !paused;
if (paused)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}
Note that Input.GetButton should only be called in Update
EDIT
The problem that you have to tap your screen to press the button. And the code jump = Input.GetButton("Fire1"); basically means "Am I tapping the screen?" So both JUMP and PAUSE are triggered.
One solution would be to put a canvas filling the whole screen under your buttons. You will trigger a JUMP action only when this canvas is clicked. So when you click your pause button, it will stop the propagation and won't click the full screen canvas, which won't trigger the jump action.
EDIT2
Try changing the lines (in the Update function) :
if (!jump)
// Read the jump input in Update so button presses aren't missed.
jump = Input.GetButton("Fire1");
for :
if (!jump && !paused)
// Read the jump input in Update so button presses aren't missed.
jump = Input.GetButton("Fire1");
I think the button event is called before the Update, see reference

One solution would be possible make sure the raycast on of pause button.

I have always done it like this, essentially surround the entire fixed update (or wherever you have your game motion) with the paused-bool
private void FixedUpdate()
{
if (paused != true){
// Read the inputs.
bool crouch = Input.GetKey(KeyCode.LeftControl);
// float h = CrossPlatformInputManager.GetAxis("Horizontal");
// Pass all parameters to the character control script.
character.Move(1, false, jump);
jump = false;
}
}

Related

How to check if a certain animation state from an animator is running?

I created an animator called "m4a4animator". Inside it, the main function is called "idle" (nothing), and other 2 states: "shoot" (mouse0) and "reload" (R). These 2 animation states are transitioned to "idle". Now, everything is working... but the only problem I have is this: if I am in the middle of reloading and and press mouse0 (shoot), the animation running state immediately changes to shoot... but I want to block that.
Now, the question: How can I stop CERTAIN animation changes while an animation is running?
Here is my animator
And here is my script:
using UnityEngine;
using System.Collections;
public class m4a4 : MonoBehaviour {
public Animator m4a4animator;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.R)) {
m4a4animator.Play("reload");
}
if (Input.GetMouseButton(0)) {
m4a4animator.Play("shoot");
}
}
}
For the legacy Animation system, Animation.IsPlaying("TheAnimatonClipName) is used to check if the animation clip is playing.
For the new Mechanim Animator system, you have to check if both anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName) and anim.GetCurrentAnimatorStateInfo(animLayer).normalizedTime < 1.0f) are true. If they are then animation name is currently playing.
This can be simplified like the function like the Animation.IsPlaying function above.
bool isPlaying(Animator anim, string stateName)
{
if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName) &&
anim.GetCurrentAnimatorStateInfo(animLayer).normalizedTime < 1.0f)
return true;
else
return false;
}
Now, everything is working... but the only problem I have is this: if
I am in the middle of reloading and and press mouse0 (shoot), the
animation running state immediately changes to shoot... but I want to
block that.
When the shoot button is pressed, check if the "reload" animation is playing. If it is, don't shoot.
public Animator m4a4animator;
int animLayer = 0;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
m4a4animator.Play("reload");
}
//Make sure we're not reloading before playing "shoot" animation
if (Input.GetMouseButton(0) && !isPlaying(m4a4animator, "reload"))
{
m4a4animator.Play("shoot");
}
}
bool isPlaying(Animator anim, string stateName)
{
if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName) &&
anim.GetCurrentAnimatorStateInfo(animLayer).normalizedTime < 1.0f)
return true;
else
return false;
}
If you need to wait for the "reload" animation to finish playing before playing the "shoot" animation then use a coroutine. This post described how to do so.
There are other threads about that: https://answers.unity.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html
if (this.animator.GetCurrentAnimatorStateInfo(0).IsName("YourAnimationName"))
{
//your code here
}
this tells you if you are in a certain state.
Animator.GetCurrentAnimatorStateInfo(0).normalizedTime
this give you the normalized time of the animation: https://docs.unity3d.com/ScriptReference/AnimationState-normalizedTime.html
Try to play with those function, I hope that solve your problem

Quit Unity3D Android app on 2 back button press

I would like my app to show the message on the first back button press as "Please touch back button again to quit the app" and when it is pressed again the app should quit. I think I have added appropriate code but it doesn't work.
The script is attached as a component to the canvas element.
The script contains the public variable which I assigned the panel(Child of canvas) UI element.
Scene hierarchy
Observed:
When I pressed the back button the text appears but only a fraction of a second and then disappear all of a sudden and the next back button press did not resulted in app quit.
Desired
On first back button press it should show the message and with in say 3 seconds if the second back button pressed the app should quit.
Relevant information:
Unity 2017.1.0f3
Here is the Code link :
https://gist.github.com/bmohanrajbit27/431221fc80e0b247649289fd136f9cfb
public class ChangeSceneScript : MonoBehaviour
{
private bool iQuit = false;
public GameObject quitobject;
void Update()
{
if (iQuit == true)
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
if (Input.GetKeyDown(KeyCode.Escape))
{
quitobject.SetActive(true);
iQuit = true;
StartCoroutine(QuitingTimer());
}
}
IEnumerator QuitingTimer()
{
yield return new WaitForSeconds(3);
iQuit = false;
quitobject.SetActive(false);
}
}
I've seen few instances where Application.Quit(); did not work on Android. When this happens, use System.Diagnostics.Process.GetCurrentProcess().Kill(); to exit out of the program.
Now, for your timer issue, start a coroutine in the Update function when input is pressed for the first time. Use a flag to make sure that this coroutine function is not started again until the last one has finished. A boolean variable is fine.
Inside, that coroutine function, don't use yield return new WaitForSeconds(3); to wait for the timer. Use a while loop with the combination of yield return null; to wait until the timer is done. Increment the timer with Time.deltaTime each frame. Now, you can easily check for the second press in that coroutine function and exit if pressed.
If you also want this to work in the Editor, you have to use UnityEditor.EditorApplication.isPlaying = false; to exit. The example below should also work in the Editor. See the comments in the code if you have any question.
public GameObject quitobject;
private bool clickedBefore = false;
void Update()
{
//Check input for the first time
if (Input.GetKeyDown(KeyCode.Escape) && !clickedBefore)
{
Debug.Log("Back Button pressed for the first time");
//Set to false so that this input is not checked again. It will be checked in the coroutine function instead
clickedBefore = true;
//Activate Quit Object
quitobject.SetActive(true);
//Start quit timer
StartCoroutine(quitingTimer());
}
}
IEnumerator quitingTimer()
{
//Wait for a frame so that Input.GetKeyDown is no longer true
yield return null;
//3 seconds timer
const float timerTime = 3f;
float counter = 0;
while (counter < timerTime)
{
//Increment counter while it is < timer time(3)
counter += Time.deltaTime;
//Check if Input is pressed again while timer is running then quit/exit if is
if (Input.GetKeyDown(KeyCode.Escape))
{
Debug.Log("Back Button pressed for the second time. EXITING.....");
Quit();
}
//Wait for a frame so that Unity does not freeze
yield return null;
}
Debug.Log("Timer finished...Back Button was NOT pressed for the second time within: '" + timerTime + "' seconds");
//Timer has finished and NO QUIT(NO second press) so deactivate
quitobject.SetActive(false);
//Reset clickedBefore so that Input can be checked again in the Update function
clickedBefore = false;
}
void Quit()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
//Application.Quit();
System.Diagnostics.Process.GetCurrentProcess().Kill();
#endif
}

Unity chat dialog system

I am trying to make a chat dialogue in unity which should show up when colliding with an object and press a key. The chat dialogue should remain until collision exit. The issue is that the chat dialogue shows up on key press when in collision but turns false in the same second so it flickers. the bool is being reset instead of remaining true while in collision.
hitColliders = Physics.OverlapSphere(transform.position, 5f);
int i = 0;
while (i < hitColliders.Length) {
if (hitColliders [i].gameObject.tag == "gameobjectCollision") {
if (Input.GetKey (KeyCode.F)) {
Debug.Log ("pressed F");
DialogueController.showConversation = true;
}
break;
}
else {
Debug.Log ("Conversation Off");
DialogueController.showConversation = false;
}
i++;
}
Also, script is being attached properly and mesh collider is set with colliding game object. Thanks.
You could do the following
Generate an event from OnTriggerEnter.
Create an ChatDialogueHandler Script and an add listener to that event.
Enable the chat dialogue box when event is raised and disable the chat dialogue box after some specific time.
You should really think about doing this off of triggers and not collision. So making a separate trigger box for each NPC that you can talk to in your game. And then make use of OnTriggerEnter and OnTriggerExit in your player script. OnTriggerEnter you can allow the conversation to start by a button click, and then OnTriggerExit you can turn off the coversation.
public void Update()
{
if (inInteractRange && Input.GetKeyDown(KeyCode.E))
{
DialogueSystemManager.Instance.StartDialogueForNpc(interactableNPCName);
}
}
private void OnTriggerEnter(Collider col)
{
inInteractRange = true;
}
private void OnTriggerExit(Collider col)
{
inInteractRange = false;
DialogueSystemManager.Instance.CloseDialogueWindow();
}

Return Gameobject to original position

I am using Unity for my project. What I am trying to do is when a button is pressed it moves a game object to a new position. Then when the same button is press again, the object returns to its original position.
My problem is with the while loop I am using; it just crashes Unity when I press this button. Am I using the while loop wrong or is there a better way of going about this?
Here is what I have so far:
public Gameobject TroubleMove;
public Gameobject TroubleAnchor;
public Gameobject TroubleMain;
public Gameobject CauseMain;
public Gameobject Cause;
public int OnOff = 1;
public void Change ()
{
switch (OnOff)
{
Case 1:
CauseMain.setActive (true);
Cause.setActive (true);
While (CauseMain == true)
{
CauseMove.transform.position = CauseAnchor.tranform.position;
}
OnOff += 1;
break;
Case 2:
CauseMain.setActive (false);
Cause.setActive (false);
OnOff _=1;
break;
}
}
I assume CauseMain is never being set to false and your while loop never exits.
Why do you need the while loop? Don't you want to change the cursor once? I would change while to if()
I will assume that the Change() is called in the OnClick() method.
Firsly, you should not use a while(true) loop when waiting for a user input or response. The while(true) loop will become a never ending loop and crash Unity / Device.
I am confuse with what your GameObjects really are but instead of while(true) loop, you should be using the Update() or FixedUpdate() method to transform the position (can be in relation to Time.DeltaTime). If I am not mistaken, this would be the skeleton of what could be done:
void Update () {
if(CauseMain) {
//TODO: Apply Transformation Changes
CauseMove.transform.position = CauseAnchor.tranform.position;
CauseMain = false;
}
}
This code snippet will only apply the transformation change whenever the player presses a button and once the position is set to "CauseMove", the check is disabled preventing an endless loop which will crash Unity.

Creating a start menu in Unity 3D

I was trying to create a start menu to my unity game when I found this script that enables a hidden sprite as soon as the game starts. The script then disables it when the player presses the left mouse button or space. When I try to make multiple sprites show up and the disappear, using the same script, only one sprite appears. I'm also trying to find a way to change the scipt so that the payer have to click on the actual sprite to disable it instead of just pressing the space key.
This is the script:
using UnityEngine;
using System.Collections;
public class StartScreen : MonoBehaviour {
static bool sawOnce = false;
// Use this for initialization
void Start () {
if(!sawOnce) {
GetComponent<SpriteRenderer>().enabled = true;
Time.timeScale = 0;
}
sawOnce = true;
}
// Update is called once per frame
void Update () {
if(Time.timeScale==0 && (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) ) {
Time.timeScale = 1;
GetComponent<SpriteRenderer>().enabled = false;
}
}
}
It sounds as if you are looking to do something like this: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
Also, I always create a separate scene for my main menu systems, as they have done in that link.

Categories