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
Related
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.
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
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).
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
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!