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).
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 have a script that recognizes swiping input. When I now start a swipe, it also triggers Input.GetMouseButtonDown(0). Can you tell me how to delete Input.GetMouseButtonDown(0) that comes from the swipe?
With that swipe the game gets paused. (with Time.timeScale = 0f;)
After resume, the action which was triggered from Input.GetMouseButtonDown(0) before pausing the game goes on.
My Ideas: Work with TouchPhase.Stationary or look for short tap?
Off the top of my head, you can either:
Call Input.ResetInputAxes; it will clear all input flags for one frame.
Set a flag that a swipe was handled, and check for it when checking for Input.GetMouseButtonDown(0) elsewhere.
Both will require ensuring that the swipe detection happens before you check for Input.GetMouseButtonDown(0).
One way of ensuring it, is by using Unity's script execution order settings.
I would recommend not using Input class at all, it causes many problems, better have separate RaycastTarget on UI with Script implementing IPointerDownHandler, IPointerUpHandler, that will ensure only one RaycastTarget at the moment is handling your touches. Input.GetMouseButton will return true even if some RaycastTarget already should have consumed your events.
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
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.
I've got a jump animation but i forgot to make a seperate 'falling' animation (and i've lost the original blender files so I can't go back and make one). At the moment, my character jumps and then the run aniamtion starts as soon as the jump is complete, so it starts running mid air.
What's the best way of solving this?
Can I pause the animation mid way through the jump?
I'm using c#.
Thanks!
You can pause the animation when the player is in the air with Animator.enabled = false; or Animator.StopPlayback(); then detect when the player is touching the floor with collider and enable your animation again.