Unity 2D Animation Not Changing States - c#

I have a pretty simple question:
I've set up the Unity Animator, with a rudimentary "Idle" state and "WalkRight" state. There's a boolean I have to transition between Idle and WalkRight.
However, for some reason, I can't get my character to transition from Idle->WalkRight when WalkRight is true.
If I set WalkRight animation to be default and the boolean is false, it will transition to Idle, however not vice versa.
Here is an image of the issue in practice:
As you can see, WalkRight = true at the bottom, and (I believe) the transition is set up correctly on the right.
What am I doing incorrectly?
Thanks!

I believe you have to have a script that changes the WalkRight boolean when the player makes the correct input. Should look something like the following:
Animator anim = GetComponent<Animator>();
anim.SetBool("WalkRight", true);

I finally figured it out. It hadn't saved my script properly, so it was using old code setting "WalkRight" to false.
Thanks!

Related

How to set animations "LoopTime" to false from script (Unity 2020.1.15f1)

I spent good time trying to find any solution but everything I find seems to be for older versions or something? Basically I want to set an AnimationClip.LoopTime from true to false or vice versa from script.
small pic of the inspector showing AnimationClip.LoopTime
I want this to let the object finish the currently running animation cycle. The GameObjects Animator.ApplyRootMotion=true to let the animation occur at the current objects position.
If I just set the Animator to enabled=false like it's advised in some posts, the animation stops right away of course and that leaves the object off position.
I get the desired info with myAnimator.GetCurrentAnimatorClipInfo(0)[0].clip.isLooping but that doesn't let me set it...
Somehow it has to be possible to just set looping=false and avoid having to create additional complicated transitions or something with the Animator?
Any help/hint is greatly appreciated!
Put an event at the end of animation, which would call function inside a script. The function could disable the Animator or stop animation, depending on your need for further enabling.

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

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

(UNITY 5) How do i Give the player control of my character once an animation is complete?

I'm currently in the middle of making an FPS game in Unity 5. I'm about to make my first cutscene and was wondering, how would I assign movement controls to the player once the cutscene has finished?
Bear in mind I may have been an idiot by making the game and the cutscene all one scene :(
You can use Animation Events to call a function in a script. For example, you could mark a boolean in the player controller to indicate whether the player is controllable or not.
You can use Animator parameters for example take a bool isMoving and set it via a script and use to transition between animation states first my player is in idle state when I set isMoving to true it will transition to moving state
set it false then will transition to another state
enter image description here

Unity - animation is not playing even thought animation.IsPlaying() is already returning 'true'

I tried playing attack animation on my 3d model using Animation.play("Attack") but somehow the animation is not playing, i already checked using Animation.IsPlaying("Attack") and it returned true, so i don't think it's the coding. Here is the code by the way:
animation.Play("Attack");
if(animation.IsPlaying("Attack")){
Debug.Log("Is Attacking");
}
I'm guessing it has something to do with the animation setting but i don't know what, anyone can help?
Assuming you are using the built-in Animation component, open the Inspector and make sure that the "Attack" animation (or any other animations) is properly added under the Animation component.
Refer to: Unity Docs - Animation

Categories

Resources