Making bool true on GetKeyDown and false once animation is done
hey i was wondering how i could get my "Slide" animation to start on DownArrow and stop after the animation is done. I was thinking of adding a delay, but i have another running animation always running when grounded so Thread.sleep won't work. ps. i'm new to unity and scripting so please go into detail.
Vector3 velocity = Vector3.zero;
public float JumpForce;
public float forwardSpeed;
private Rigidbody2D myRigidbody;
public bool Slide;
public bool Grounded;
public LayerMask whatIsGround;
public Transform GroundCheck;
public float GroundCheckRadius;
private Animator MyAnimator;
// Use this for initialization
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
MyAnimator = GetComponent<Animator> ();
}
// Do Graphic & Input updates here
void Update()
{
Grounded = Physics2D.OverlapCircle (GroundCheck.position, GroundCheckRadius, whatIsGround);
myRigidbody.velocity = new Vector2 (forwardSpeed, myRigidbody.velocity.y);
if (Input.GetKeyDown (KeyCode.UpArrow) || Input.GetMouseButtonDown (0)) {
if (Grounded) {
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, JumpForce);
}
}
MyAnimator.SetBool ("Grounded", Grounded);
if (Input.GetKeyDown (KeyCode.DownArrow) || Input.GetMouseButtonDown (1))
{
if (Grounded) {
}
}
}
Have you considered adding method to your script that sets your bool to false and calling it via event in animation as it ends?
public AnimationEvent Sliding() { $$anonymous$$yAnimator.SetBool ("Slide", false); } this is what i came up with but it says it doesn't return a value, do you know what i would have to add?
I don't really understand what you want to get. Please, describe how it should work in general. So you have a player, he's running and you want him to?..
So mark this answer as a correct, please. Good luck!
You know you can set the triggers in mecanim, so you can use GetComponent().SetBool("whatever", true) and you can link it up with your slide animation when its true, it switches from grounded to slide, and when the slide is done it switches over to grounded again. you can do this all in mecanim in the blend tree states.
Answer by LMan · Jan 05, 2016 at 12:22 AM
You can get the current animator state, and access the normalized time variable to tell how far along the state is. (0 being the start, 1 being the end.) bear in mind that a transition may prevent a state from reaching 1, so you may have to check for something close to the end, like .95.
so you may have to check for something close to the end, like .95.
And if the value will change from 0.94 to 0 (i.e. new animation starts) you event not happens. That's not how you should treat floats. Unity provides several approaches to solve this task. Your method is not a best practice.
All it requires is looking at the transition length in mecanim to get the exact value reached. This method is viable and in common use.
Can you please provide some examples when this approach is viable?
This question mentions the normalized time method twice.
You will also find this method in the $$anonymous$$ecanim example package from Unity. (match target example).
The duration of a transition is not a dynamic number, you can know exactly how long a transition will take, and what frame it will begin on, and therefore you can check normalized time for the last frame. This method is not as flexible as events, because lengthening the transition can break it if you don't change the float you're looking for. Still it can be simpler and faster to implement.
This is a common practice and I have found it in multiple popular and well reviewed asset store packages, I've also used it myself.
Answer by incorrect · Jan 06, 2016 at 01:25 AM
Ok. So to make what I've suggested you can add a simple public method to a script you want to switch bool in.
public void SwitchMyBool()
{
myBool = false; //or true, or whatever you need here to be done
}
And after that you can call this method from animation as it is described in Using Animation Events page of Manual.
Will it work for you?