How to play animations while the game is paused (timescale = 0) - c#

I am trying to pause a game in Unity using by setting timeScale to 0 when the pause panel comes up, and setting it back to 1 when the panel is disabled. The problem I am having is that when I Pause, the buttons on the panel are not showing their animation as the time scale is 0. Is there anyway around this? Or should I find another way to do the pause without using the timescale?

In your Animator component look for Update Mode option and select Unscaled Time. Like this :

A possibility is to maintain state of your game using Enum. e.g., define an enum as:
enum GameState
{
Started,
Loading,
Playing,
Paused,
Completed
}
Declare a variable of type GameState in your session or any place where access is easy to you, then compare:
if(currentGameState == GameState.Playing)
{
// Play logic here
}

To fix your animations issue . Simply make the animation to play in unscaled time. That way the animation plays even if Time.timeScale is 0. You can do this by setting the update mode of the animator to Unscaled time.

Related

The animation state .. could not be played because it couldn't be found?

I am trying to start an animation by pressing a key on my keyboard.
For this I am using the following code:
[HideInInspector] public Animation animation;
private void Start()
{
animation = GetComponent<Animation>();
}
private void Update()
{
if (Input.GetKeyDown("1"))
{
animation.Play("AnimationName");
}
}
This gives me the following error:
The animation state AnimationName could not be played because it
couldn't be found!
Although, the animation DOES start, but how can I clear this error?
I think the only possible explaination is that your animation clip is not marked as Legacy.
https://www.unity3dtips.com/zh/the-animation-state-could-not-be-played-because-it-couldnt-be-found/
EDIT with step by step here:
Animations must be marked as legacy to be used with the animation component.
In the project window select the animation clips you’re trying to play.
Set the inspector to debug mode which exposes hidden variables, in this case it’ll make the “Legacy” checkbox appear.Unity Inspector Debug Mode
Tick the “Legacy” checkbox and change the inspector back to normal mode.
Unity Inspector Animation Clip Legacy Checkbox
But there is one thing you should know: you should avoid, if possible, the old legacy way to play animation.
Learn how to use an Animator component. Just create an animator, assign your animation clip, decide a boolean for start the animation in the animator component and set it to true in your code (for basic use).
Sure there is some things to do beside code but it will be easier to control your animation, bleand different animations and change them to your needs (it's for example much easier to loop or stop the animation at some point).
It would be also easier to understand when it start and when is playing.
Another thing to check is the legacy 'Animation' component on your gameObject that is supposed to perform the animation. In my case things were marked as legacy for the animation on the imported animation but somehow when adjusting settings on my import it dropped the animation it was complaining about from the actual 'Animation' component so once I added it things worked as expected
Inspector in Debug mode showed my animation marked as 'Legacy' true
My Animation component showed the missing animation

How to reset sequence of animations when target is lost

I am using Unity and Vuforia, and would like to make an animation on AR object that starts when target is found and resets when target is lost so that, when target is found again the animation starts from the beginning.
In order to make animation start only once target is found, I selected the option "Cull Completely" in the Culling Mode property of the animator component. But I can't manage to reset the animation when target is lost ! I have tried modifying the DefaultTrackableEventHandler script (the OnTrackingFound and OnTrackingLost methods) but it doesn't work.
So far I have tried :
Animation[] animationComponents = GetComponentsInChildren<Animation>();
foreach (Animation component in animationComponents)
{
component.Stop();
}
and some variations (animation name as a parameter of Stop method, animator component instead of animation component, ...).
Does someone know how to do this ?
Thanks for your attention :)
I know a way of doing what you want in a simpler way. Look for https://docs.unity3d.com/Manual/class-State.html
You will get the same results but in a different way using the animation states.
Then go to the animator window
And you can build something like that. Those are transitions between states, associated to an animation.
Take a look at this too https://docs.unity3d.com/Manual/class-Transition.html
So in your script you can access to the Animator with:
Animator MyAnimator = GetComponent<Animator>();
And in somewhere over your conditions in the game, you can do this:
MyAnimator.Play("Attack");
If that animation doesn't have a transition, it will your decision to make it a loop animation (it will repeat when ends) or it will stay in the position of the last frame of the animation.
With some conditions you might want to have, like the one in your question, you could do something like this.
if (Vector3.Distance(this.transform.position, myTarget.transform.position) >= 2f) {
MyAnimator.Play("Idle");
}
Warning: That Vector3.Distance may not be the best and fastest way of doing it, it's just as an example. Remember to always try to optimize your code with your project requirements.

issue using "Any State" in Unity Animator

So, i am making a little 2D game, and in this game the player can snowboard, so, i made the player animator, and i wanted the player to snowboard doesn't matter the state, so i used the "Any State" state to transition the current animation to the "9_Snowboarding" animation using a bool called "isSnowboarding", and it worked fine.
The problem began when i wanted the player to jump, i created the jump animation, and i created a bool to make the transition happen called "isJumping", and i set the bool to true by code.
Instead of transitioning to the animation and playing it, the animator controller keeps transitioning the "9_Snowboarding" to the "10_SnowboardJumping" multiple times, and i dont know how to solve this.
Alternatively, if you absolutely need a bool there for some reason, you can disable the Option "Can Transition To Self" in the Transition settings of the Any State to your State Die.
Particularly in your case you need to use a "trigger" instead of a "bool" parameter.
The problem with bool is that isJumping is always set to true so your condition keeps on matching and you keep on transitioning to the same animation.
"Triggers" on the other hand will disable once used. So try adding something like Jump trigger and set that in your code

How to stop the animation halfway and transit in Unity?

I have this structure of animations:https://i.stack.imgur.com/Rv0fq.png
The idle state runs in a loop until the user presses "space",then parameter named "Jump" becomes true and the transition between idle and Jump becomes active.But before it becomes active user has to wait for "idle" to finish.How can i make it so when user presses space,idle automatically stops and transits to Jump? I tried making 'animation' stop like this:
if(Input.GetKeyDown("space")){
anim.Stop();
animator.SetBool("Jump",true);
}
But it didn't work.
Ok, I warn you the jump is not that easy.
You will need to know if the player is on ground if you don't want your player to be able to jump in the air and many other things.
Anyway to your question.
To avoid exit time on animation you should uncheck the "Has exit time" value on the animator arrow between an animation and the other one.
Your current code just stop the animator but that's not what you really want... you want it to return to Idle.
The best thing you can do is:
- From "Any state" to "Jump" (you should create a blend tree between Jump and Fall)--->in the arrow uncheck "Has exit time" and set up a new Bool "JumpBool" (for example).
From "Entry" to "Idle"
From "Jump" to "Idle" when "JumpBool" is false.
In your code:
if(Input.GetKeyDown("space") && !OnGround()){
animator.SetBool("JumpBool",true);
}
(you should create your OnGround method).
else if(OnGround()){
animator.SetBool("JumpBool",false);
}
Of course this is almost pseudo-code. Like I said you should create a BlendTree for Jump and Fall where you should be able to tell if you need to play the jump animation of just the fall animation (maybe with a float depending on your corrent rigidbody .velocity.y speed).

Run new animation only when previous one is finished

I'm new to Unity and I'm struggling with configuring my animations properly. I have attack animation which must be played if you press the spacebar once and if you hold it it should play until you release. My problem is that once you release the spacebar the animator instantly returns to the previous state. I've read some answer on the Unity Q&A site but they are all concerning a script that runs the animations, I don't use any script to run mine I'm simply setting them up in the animator. How can I fix this ? The way I change the animation is by switching the value of a boolean variable like this :
if(Input.GetKeyDown(KeyCode.Space))
{
IsAttacking = true;
}
if(Input.GetKeyUp(KeyCode.Space))
{
IsAttacking = false;
}
playerAnimator.SetBool("IsAttacking", IsAttacking);
My project is in 2D if it matters.
Click this ->
Exit Time
Inside of the animator you have your clips. Clicking on a clip with bring up this inspector view. You'll want to check Has Exit Time'.
Then you'll want to set your exit time to 1 and your transition duration to 0.
By setting those values, you're telling the animator that this animation MUST finish before moving on to the next one.

Categories

Resources