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.
Related
Hi, I am currently making an attack animation when the mousebutton (0) or left click has been used for a single click only with no holding, so when I click the left click on the mouse, everything went okay but when holding it, the animation continues but then will stop on the last clip of my animation clip unless I release the left click of my mouse, which sets the bool of my attack animation to true, and everything goes to normal.
So what should I do?
Here's my video LINK so I could describe it to you guys well, I hope someone could help me :).
https://streamable.com/wfnw5f
if(Input.GetMouseButtonDown(0))
{
anim.SetBool("attack", true);
}
if(Input.GetMouseButtonUp(0) )
{
anim.SetBool("attack", false);
}
You need it to be set to false as soon as it is true. Luckily, triggers do exactly that.
Make attack a trigger instead of a bool then use SetTrigger instead of SetBool.
From the documentation:
Unlike bools which have the same true/false option, Triggers have a true option which automatically returns back to false. A typical example might be to have a Jump option. If this option is entered during run-time the character will jump. At the end of the Jump the previous motion (perhaps a walk or run state) will be returned to.
I am trying to make a character play a specific animation when pressing the R button. I would like him to play the whole animation and then stop once it has run a cycle.
The issue now is that when I press "R" the animation only plays for a frame and no animation plays at all.
I started with this statement below.
void Update ()
{
if (Input.GetKeyDown(KeyCode.R))
{
anim.Play("Ready");
}
}
I then read that it might be better to create a bool instead, if the bool is true -> play Animation.
public bool ready;
void Update ()
{
if (Input.GetKeyDown(KeyCode.R))
{
ready = !ready;
}
if (ready == true)
{
anim.Play("Ready");
}
}
When bool ready is true, the animation plays one frame and then the character becomes static. If the bool the gets false again, the character returns to his idle animation.
My latest attempt to making this work is by structuring the code like this:
if (Input.GetKeyDown(KeyCode.R))
{
ready = !ready;
if(ready == true)
{
anim.Play("Ready");
}
}
However this only causes the animation to play a very small amount of frames until going back to the idle animation. Even though ready stays true.
I am not using the "Animator component" but rather the "Animation component" instead. This are the settings of it: Animation Component
Could it be the void Update() causing the issue?
Also, is there a way to make sure the animation only plays for one cycle and then stops?
Your method looks valid. Are there chances that your imported animation isn't?
You should use the Animator as suggested:
drop your clips in the Animator window,
right click on the default state > "make transition", release on the next clip,
create a parameter of type bool named "ready",
assign this parameter in the transition from idle to the next state (select the arrow and click the "+" button at the bottom of the component in the Inspector window, select your parameter),
in your script, you just have to call:
myAnimator.SetBool("ready", true);
There are a lot of benefits of using the Animator :
handle many animation clips, automatic transitions, preview, and more features with "humanoid" rigs (in the model import settings)...
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 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.