How can I check when animator state has finished playing animation? - c#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimFlyertransportationController : MonoBehaviour
{
public GameObject JetFlame;
public bool turnOffOn = false;
public bool land = false;
private Animator[] animators;
// Start is called before the first frame update
void Start()
{
TurnAllAnimtorsOff(transform, turnOffOn);
if (turnOffOn)
{
JetFlame.SetActive(true);
}
else
{
JetFlame.SetActive(false);
}
}
private void TurnAllAnimtorsOff(Transform root, bool onOff)
{
animators = root.GetComponentsInChildren<Animator>(true);
foreach (Animator a in animators)
{
if (onOff)
{
a.enabled = true;
}
else
{
a.enabled = false;
}
}
}
private void OnValidate()
{
TurnAllAnimtorsOff(transform, turnOffOn);
if(land == true && turnOffOn)
{
animators[0].Play("Anim_Flyer_Land");
TurnAllAnimtorsOff(transform, false);
JetFlame.SetActive(false);
turnOffOn = false;
land = false;
}
else if(land == false && turnOffOn)
{
animators[0].Play("Anim_Flyer_Takeoff");
}
if (JetFlame) JetFlame.SetActive(turnOffOn);
}
}
// Anim_Flyer_Takeoff
// Anim_Flyer_Land
At this place I need to check if the "Anim_Flyer_Land" animation finished playing then set everything to false :
if(land == true && turnOffOn)
{
animators[0].Play("Anim_Flyer_Land");
TurnAllAnimtorsOff(transform, false);
JetFlame.SetActive(false);
turnOffOn = false;
land = false;
}
Now it's setting everything to false before the animation has finished playing.
The main goal is to use two flags or one is even better when true set everything to true the animators and the flame the default state machine state is the Anim_Flyer_Takeoff then when setting the flag to false first play the land animation and then turn off disable everything and then if flag is true again first enable everything and play the take off.
Screenshot of the animator controller :
The idea is to make a flag for take off/land
I tried this the checking script if the animation still playing :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckAnimationEnd : MonoBehaviour
{
public GameObject _animator;
public string animStateName;
private Animator _ani;
// Start is called before the first frame update
void Start()
{
_ani = _animator.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (animStateName != null && animStateName != "" && _ani != null)
IsPlayingAnimation();
}
public bool IsPlayingAnimation()
{
bool IsOffLand = false;
if (_ani != null)
{
if (!_ani.GetCurrentAnimatorStateInfo(0).IsName(animStateName))
{
IsOffLand = true;
}
else
{
IsOffLand = false;
}
}
return IsOffLand;
}
}
And then :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimFlyertransportationController : MonoBehaviour
{
public GameObject JetFlame;
public CheckAnimationEnd checkAnimEnd;
public bool turnOffOn = false;
private Animator[] animators;
// Start is called before the first frame update
void Start()
{
TurnAllAnimtorsOff(transform, turnOffOn);
if (turnOffOn)
{
JetFlame.SetActive(true);
}
else
{
JetFlame.SetActive(false);
}
}
private void TurnAllAnimtorsOff(Transform root, bool onOff)
{
animators = root.GetComponentsInChildren<Animator>(true);
foreach (Animator a in animators)
{
if (onOff)
{
a.enabled = true;
}
else
{
a.enabled = false;
}
}
}
private void OnValidate()
{
TurnAllAnimtorsOff(transform, turnOffOn);
if (JetFlame) JetFlame.SetActive(turnOffOn);
Land();
TakeOff();
}
private void Land()
{
if (turnOffOn == false)
{
checkAnimEnd.animStateName = "Anim_Flyer_Takeoff";
animators[0].Play("Anim_Flyer_Land");
if (checkAnimEnd.IsPlayingAnimation())
{
TurnAllAnimtorsOff(transform, false);
JetFlame.SetActive(false);
turnOffOn = false;
}
}
}
private void TakeOff()
{
if(turnOffOn)
{
checkAnimEnd.animStateName = "Anim_Flyer_Land";
animators[0].Play("Anim_Flyer_Takeoff");
if (checkAnimEnd.IsPlayingAnimation())
{
TurnAllAnimtorsOff(transform, true);
JetFlame.SetActive(true);
turnOffOn = true;
}
}
}
}
but everything is messed now. when I set the TurnOffOn to be true the spacecraft start from up and land and then when set it false it's taking off and everything turn on/off the animators and fire I messed all the flags and both scripts.

you can add (animation event) at the end of the animation state then put all the commands that you want on the animation event's function.

Related

Input.GetKey("f") does not do anything when f i pressed

Within my, if function, the Input.GetKey("t") command does not work.
The Restart method is called when I remove the contents from the if function and place them outside.
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
bool gameHasEnded = false;
public float restartDelay = 2f;
public GameObject LevelFailedUI;
public GameObject LevelCompleteUI;
public void CompleteLevel ()
{
if (LevelFailedUI == true)
{
LevelCompleteUI.SetActive(true);
}
}
public void EndGame ()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
LevelFailedUI.SetActive(true);
if (Input.GetKey("t")) //nothing happens when "t" is pressed.
{
Invoke("Restart", restartDelay);
}
}
}
void Restart ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
LevelFailedUI.SetActive(false);
}
}
It won't work because unity has to be inside the function and also hasn't reached the check before reaching the if statement and you press the button during one frame which is basically impossible. You have to check for input constantly that is why it is done inside the Update function.
void Update() {
if (Input.GetKey(KeyCode.T) && gameHasEnded) {
Invoke("Restart", restartDelay);
}
}
public void EndGame() {
if (gameHasEnded == false) {
gameHasEnded = true;
LevelFailedUI.SetActive(true);
}
}

How do I fix my code for blocking showing/hiding menu?

I got problem with pause menu holding esc key fix.
I writed simple script that blocks showing/hiding pause menu for 5 seconds, but it doesn't work.
Can someone tell me what I'm doing wrong or fix my code?
(Here I'm writing cuz i cant post my question lol)
Here is code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MenuController : MonoBehaviour
{
public static bool IsGamePaused = false;
public static bool IsClickingBlocked = false;
public GameObject zagadka1;
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.T))
{
if (IsGamePaused)
{
if (IsClickingBlocked == false) {
ClickStopCounter();
zagadka1Start();
}
}
else
{
if (IsClickingBlocked == false)
{
zagadka1Stop();
ClickStopCounter();
}
}
}
}
IEnumerator ClickStopCounter()
{
IsClickingBlocked = true;
yield return new WaitForSeconds(5);
IsClickingBlocked = false;
}
public void zagadka1Start()
{
zagadka1.SetActive(false);
Time.timeScale = 1f;
IsGamePaused = false;
Cursor.lockState = CursorLockMode.Locked;
}
void zagadka1Stop()
{
zagadka1.SetActive(true);
Time.timeScale = 0f;
IsGamePaused = true;
Cursor.lockState = CursorLockMode.None;
}
You should use StartCoroutine when calling the Coroutine.
Try this one. StartCoroutine(ClickStopCounter())

How can I fade out the ui text when enable it false?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveGameMessage : MonoBehaviour
{
public Text text;
public float speed;
public bool startFading = false;
public bool enableText = false;
private Color textColor;
private void Start()
{
text = GetComponent<Text>();
textColor = text.color;
}
private void Update()
{
if (startFading == true)
{
if (enableText == true)
{
text.enabled = true;
}
textColor.a = (Mathf.Sin(Time.time * speed) + 1.0f) / 2.0f;
text.color = textColor;
}
else
{
text.enabled = false;
}
}
}
And using it :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayingInGameScenesController : MonoBehaviour
{
public CinemachineFreeLook[] freeLookcameras;
public LockController lockController;
public GameObject uiSceneText;
public GameObject player;
public float transitionSpeed = 5f;
public float thresHold = 0.1f;
public bool talkingProcess = true;
public SaveGameMessage saveGameMessage;
private bool newGame = true;
private void Start()
{
}
public void PlayingSceneInGame()
{
PlayingSceneStatesControls(true);
StartCoroutine(ScenePlayingTime());
}
private void Update()
{
if (SceneManager.GetActiveScene().name != "Main Menu" && newGame == true)
{
PlayingSceneInGame();
newGame = false;
}
}
private void LateUpdate()
{
}
private void PlayingSceneStatesControls(bool LockState)
{
lockController.LockControl(LockState);
if (LockState == true)
{
uiSceneText.SetActive(true);
}
else
{
talkingProcess = false;
uiSceneText.SetActive(false);
}
}
IEnumerator ScenePlayingTime()
{
yield return new WaitForSeconds(10);
PlayingSceneStatesControls(false);
freeLookcameras[0].enabled = false;
freeLookcameras[1].enabled = true;
var brain = Camera.main.GetComponent<CinemachineBrain>().m_DefaultBlend.m_Time;
saveGameMessage.enableText = true;
saveGameMessage.startFading = true;
player.GetComponent<Player>().SavePlayer();
StartCoroutine(FakeSave());
}
IEnumerator FakeSave()
{
yield return new WaitForSeconds(7);
saveGameMessage.startFading = false;
}
}
At the bottom I'm making fake for 7 seconds the problem is when the fake finish the ui text enable false at once. In the SaveGameMessage script at the bottom I'm doing :
text.enabled = false;
but that is ugly it's just stop the fade in out effect at once. I want somehow to enable it false but before that to finish the current fading so the text enabled false will be smooth fading out and ot just suddenly stopping the fading effect.
Before you disable it run an animation to fade it out. this animation should first be made or maybe you can find one online.
then just run the animation before setting it to false.

Unity - Dialogue pops up

In Unity:
Could anybody tell me what I did wrong. I wanted to pop up a dialogue after you are nearby a charecter but somehow my code doesn't really work.
public class Interactable : MonoBehaviour {
[HideInInspector]
public NavMeshAgent playerAgent;
private bool hasInteracted;
public virtual void MoveToIneraction(NavMeshAgent playerAgent)
{
hasInteracted = false;
this.playerAgent = playerAgent;
playerAgent.stoppingDistance = 2.3f;
playerAgent.destination = this.transform.position;
Interact ();
}
void Update()
{
if (!!hasInteracted && playerAgent != null && playerAgent.pathPending)
{
if(playerAgent.remainingDistance <= playerAgent.stoppingDistance)
{
Interact();
hasInteracted = true;
}
}
}
public virtual void Interact()
{
Debug.Log("Interacted");
}
}
!!hasInteracted
It should be !hasInteracted, I guess

Unity Variable has not been assigned Error

I cannot figure out the issue, I deleted then remade the PlayTab object, but it no longer works... It is telling me it has not been assigned, but it has to my knowledge. Relatively new to Unity programming, any help would be great, the script below.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Audio;
public class MainMenu : MonoBehaviour {
public GameObject Buttons;
public GameObject CreditsPanel;
public GameObject QuitPanel;
public GameObject Options;
public GameObject PlayTab;
public Slider audiosl;
public Slider graphicsl;
public Toggle fullscreen;
public string SceneName;
void Start (){
QualitySettings.SetQualityLevel (100);
//(int)PlayerPrefs.GetFloat("Quality")
AudioListener.volume = 100;
//PlayerPrefs.GetFloat("Volume");
int qualityLevel = QualitySettings.GetQualityLevel();
audiosl.value = AudioListener.volume;
graphicsl.value = qualityLevel;
}
void Update (){
Debug.Log ("Update");
AudioListener.volume = audiosl.value;
QualitySettings.SetQualityLevel ((int)graphicsl.value);
}
public void InGame(bool a){
if (a == true) {
Application.LoadLevel (SceneName);
} else {
//continue
}
}
public void Play(bool a){
Debug.Log ("Inside Play Function");
if (a == true) {
Debug.Log ("Inside If Statment");
PlayTab.SetActive(a);
Buttons.SetActive(!a);
Animation pl = PlayTab.GetComponent<Animation>();
pl.Play("EnterPlayMenu");
}else {
Debug.Log ("Else'd");
PlayTab.SetActive(a);
}
}
public void ShowMenu(bool a){
}
public void Option(bool a){
if (a == true) {
Options.SetActive(a);
Buttons.SetActive(!a);
Animation Opt = Options.GetComponent<Animation>();
Opt.Play("OptionEnter");
}if (a == false) {
Animation d = Buttons.GetComponent<Animation> ();
d.Play ("mainbuttonenter");
Options.SetActive (false);
}
}
public void Credits(bool a){
if (a == true) {
CreditsPanel.SetActive(a);
Buttons.SetActive(!a);
Animation cr = CreditsPanel.GetComponent<Animation>();
cr.Play("EnterCredits");
}else {
CreditsPanel.SetActive(a);
}
}
public void Quit(bool a){
if (a == true) {
QuitPanel.SetActive(a);
Buttons.SetActive(!a);
Animation q = QuitPanel.GetComponent<Animation>();
q.Play("EnterQuit");
}else {
QuitPanel.SetActive(a);
}
}
public void Exit(bool a){
if (a == false) {
Option(false);
Buttons.SetActive(true);
CreditsPanel.SetActive(false);
QuitPanel.SetActive(false);
Options.SetActive(false);
PlayTab.SetActive(false);
saveSettings();
}
if (a == true) {
Application.Quit();
}
}
public void saveSettings(){
PlayerPrefs.SetFloat ("Quality", QualitySettings.GetQualityLevel ());
PlayerPrefs.SetFloat ("Volume", AudioListener.volume);
}
public void FullScreen(bool a){
if (Screen.fullScreen == a) {
Screen.fullScreen = !a;
} else {
Screen.fullScreen = a;
}
}
}
As far as I see, you have only defined the playTab variable, but not assigned it.
EDIT:
Try displaying it at start, then automatically hiding it via code. Probably Unity don't initialize objects that are not visible on scene from the start.

Categories

Resources