Unity: Animation won't play on trigger - unity3d

Im having trouble to let Unity play my animations.
I gave my character an Animator. In this Animator I have this Animations:
The trigger between these Animations are working fine and are running correctly on play. The idle animation it self is played, while i can't get it to play the walk or the jump Animation in the game (both work fine in the inspector).
When on play the triggers are triggered, it even shows the blue bar under the walking or the jumping box, but the animation itself just wont be played in the game.
Here is the code I use to call the triggers:
void Update()
{
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow)|| Input.GetKey(KeyCode.A)||Input.GetKey(KeyCode.D))
{
anim.SetBool("isWalk", true);
}
else
{
anim.SetBool("isWalk", false);
}
if (Input.GetKey(KeyCode.Space))
{
anim.SetBool("isJump",true);
}
else
{
anim.SetBool("isJump", false);
}
}
And the Transition Conditions (but these are working fine for me):
idle->jump: isJump =true,
jump->idle: isJump =fals,
idle->walk: isWalk =true,
walk->idle: isWalk =false,
screenshot of idle-> walk:
screenshot of walk->idle:
I hope someone can help me with this and please excuse my possibly bad english...

I had the same issue.
Solved it by unchecking 'Can Transition To Self' inside the transition pointing to the state that didn't play.

Related

How to wait until animation is finished when using Animator?

This is a beginner question that should be available from the first google result but I can't find it.
I have this method that plays an animation using an Animator:
IEnumerator popDownAnimation(string c)
{
animator = GetComponent<Animator>();
animator.Play("Pop Down Animation");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 && !animator.IsInTransition(0));
animator.Play("New State");
}
Can't remember where I got it from, but yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 && !animator.IsInTransition(0)); isn't working. It's instantly jumping from "Pop Down Animation" to "New State", so the "Pop Down Animation" doesn't have a chance to play.
Try this
IEnumerator popDownAnimation(string c)
{
animator = GetComponent<Animator>();
animator.Play("Pop Down Animation");
float animationLength = animator.GetCurrentAnimatorStateInfo(0).length;
yield return new WaitForSecondsRealtime(animationLength);
animator.Play("New State");
}
However, like #NotVeryGoodAtThis mentioned, I wouldn't do this in code unless you have a specific reason why you can't make it work using animation events or using parameters within the animator to transition between animation states. Designing your animation using Unity's Animator window and using states, parameters, and transitions will be easier to manage than doing it through code, especially as your animators become more complicated.
I would suggest using Animation events to do whatever you need. Just remember to make a public method and add the animation event at the end of your animation.

Unity Particles Aren't working with custom script

I started my game dev journey a few weeks ago and I am enjoying it, but sometimes it can get frustrating when things do not work.
I wrote a very basic code for particle system, if we press space then particle should play. The problem is that its not playing, when I hit play it doesn't work for some reason. When I click the particle in the scene then it works and it also works when I check on the play on awake
The Code:
[SerializeField] ParticleSystem engineBoostParticle;
[SerializeField] ParticleSystem sideEngineParticles;
void Start()
{
}
void Update()
{
ThrustingInput();
}
void ThrustingInput()
{
if (Input.GetKey(KeyCode.Space))
{
if (!engineBoostParticle.isPlaying)
{
engineBoostParticle.Play();
}
}
else
{
AS.Stop();
engineBoostParticle.Stop();
}
}
You want to check engineBoostParticle.isEmitting as well as isPlaying in your if statement.
It's possible that your system is playing, but not emitting (because the loop is over), so make sure the particle system loops properly as well.

Unity Mecanim. How to reset animation before disabling a gameobject

Unity 2019.3.7f1
..
The artist I'm working with has made some animations for some UI stuff using the mecanim system Animator component.
I fire the animation with
Animator.SetTrigger("ParamaterName")
The gameobjects with the animator components, which are being animated, can get disabled by some of my scripts.
This can happen while the animation is playing..
So if the animation starts playing, and lets say the animation has made a button get bigger, then the gameobject gets disabled. When the gameobject is re-enabled (the animation is no longer playing) it is still big..
Is there a way to tell the Animator to go back to normal?
I have tried stuff like this in onEnable and OnDisable in a script on the GameObject
Animator.keepAnimatorControllerStateOnDisable = false
Animator.Play("normalState",0,0f);
Animator.playbackTime = 0f;
Animator.Update(0f);
This mecannim thing just seems like a black box as to how it works. I'm not familiar with it as I've always just used my own library to animate stuff that I've been using for donkeys years.
EDIT:
This was the solution...
private void Start()
{
Animator.keepAnimatorControllerStateOnDisable = true;
}
private void OnDisable()
{
Animator.Play("normalState",0,0f);
}
Make sure your keepAnimatorControllerStateOnDisable isn't set to true. Instead, you should set to false to clear the current state of the Animator controller
Animator.keepAnimatorControllerStateOnDisable = false
This way whenever you disable an object with an Animator, the Animator states and all its parameters go back to default.
Update
From your comments was able to understand the default is to have the button bigger. So, it's actually the other way around (set it to true, instead of false).
As you mention, write the following code
private void Start()
{
Animator.keepAnimatorControllerStateOnDisable = true;
}
private void OnDisable()
{
Animator.Play("normalState",0,0f);
}

Animation Trigger set in IEnumerator causes Animation to get stuck

private IEnumerator reviveCountdown() {
timeLeft = 5;
while (timeLeft >= 0) {
reviveAnim.ResetTrigger ("AdReviveTrigger");
reviveAnim.SetTrigger ("AdReviveTrigger");
reviveCountdownText.text = timeLeft.ToString();
yield return new WaitForSecondsRealtime(1.0f);
timeLeft--;
}
}
I'm using the above IEnumerator to show a timer that counts down from 5 to 0, and animates the text each time it counts down. The countdown works correctly. The problem I'm having is the animation IS TRIGGERED but never actually plays because it gets stuck on the animation state, as shown in the screenshot below. It sits in the AdRevive state for the entire 5 seconds but never plays. The transition from Idle is a simple trigger set in the code. The animation works if I play it manually in the Unity editor. Anyone know why it gets stuck?
I solved my own issue. The Time.timeScale was set to 0 because the game was paused during this animation, so it would never play. To fix, I set the animationUpdateMode to UnscaledTime.

How to prevent mouse clicks from passing through GUI controls and playing animation in Unity3D [duplicate]

This question already has answers here:
Detect mouse clicked on GUI
(3 answers)
Closed 4 years ago.
Description
Hi Guys need your help I faced problems with mouse clicks which pass through UI panel in Unity, that is, I have created pause menu and when I click Resume button, the game gets unpaused and the player plays Attack animation which is undesirable.What I want is when I click Resume button, Attack animation should not be played. The same problem if I just click on panel not necessarily a button and the more I click on UI panel the more Attack animation is played after I exit pause menu. Moreover, I have searched for solutions to this issue and was suggeted to use event system and event triggers but since my knowledge of Unity is at beginner level I could not properly implement it. Please guys help and sorry for my English if it is not clear)) Here is the code that I use:
The code:
using UnityEngine;
using UnityEngine.EventSystems;
public class PauseMenu : MonoBehaviour {
public static bool IsPaused = false;
public GameObject pauseMenuUI;
public GameObject Player;
private bool state;
private void Update() {
//When Escape button is clicked, the game has to freeze and pause menu has to pop up
if (Input.GetKeyDown(KeyCode.Escape)) {
if (IsPaused) {
Resume();
}
else {
Pause();
}
}
}
//Code for Resume button
public void Resume() {
//I was suggested to use event system but no result Attack animation still plays once I exit pause menu
if (EventSystem.current.IsPointerOverGameObject()) {
Player.GetComponent<Animator>().ResetTrigger("Attack");
}
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
IsPaused = false;
}
//this method is responsible for freezing the game and showing UI panel
private void Pause() {
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
IsPaused = true;
}
//The code for Quit button
public void QuitGame() {
Application.Quit();
}
}
Im not sure if i understood your problem, but it sounds like somewhere in your code you start an attack when the player does a left click.
Now your problem is that this code is also executed when the player clicks on a UI element, for example in this case the Resume button?
You tried to fix this problem, by resetting the attack trigger of the animator, i think it would be a better solution to prevent the attack from starting instead of trying to reset it later.
EventSystem.current.IsPointerOverGameObject() returns true if the mouse is over an UI element.
So you can use it to modify your code where you start your attack:
... add this check in your code where you want to start the attack
if(EventSystem.current.IsPointerOverGameObject() == false)
{
// add your code to start your attack
}
...
Now your attack will only start if you are not over a UI element