- Home /
How to smoothly change from opening a door animation to closing a door animation
Been searching for an answer for this, but guess I have not found the right answer to my animations.
I have one door, that has one trigger.
I have two animations:
GlassDoorAtStart_GO_Animation_OPEN, (which slides the door up).
GlassDoorAtStart_GO_Animation_CLOSE, (which slides the door down).
I have recently tried adding two parameters in the animator:
(bool) isOpening and (bool) isClosing as two parameters in the animator. What I am trying to achieve, is the following: The Glass door is closed initially. When the player walks into the trigger, the door starts opening, but if he walks out of the trigger, then I want the door to change over to the closing door animation. Right now, when walking into the trigger, the OpenDoorAnimation starts playing (to open the door), but if the player walks out of the trigger, in the middle of the OpenDoor animation, the CloseDoor animation starts at the beginning (which the GlassDoor goes to the top of the door opening and starts its slide down animation.
=================
This is my code:
//=========================================================
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
if (HasPlayerEnteredTriggerBefore == false)
{
HasPlayerEnteredTrigger = true;
HasPlayerEnteredTriggerBefore = true;
if (!AnimatorIsPlaying(GlassDoorAtStart_GO_Animator))
{
StartCoroutine(OpenDoorAnimationWaitTime(TimeToOpenDoor));
}
}
}
}
//======================================
public void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
if (HasPlayerEnteredTriggerBefore == true)
{
if (!AnimatorIsPlaying(GlassDoorAtStart_GO_Animator))
{
StartCoroutine(CloseDoorAnimationWaitTime(TimeToCloseDoor));
HasPlayerEnteredTriggerBefore = false;
}
}
}
}
//================================================
public bool AnimatorIsPlaying(Animator ThisAnimator)
{
// return TRUE if animation length is > the animation normalized time.
// What we need to determine is:
// Is the current animation that is playing finished?
bool RetVal = false;
float animationLength = ThisAnimator.GetCurrentAnimatorStateInfo(0).length;
float normalizedTime = ThisAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime;
if (animationLength > normalizedTime)
{
RetVal = true;
}
else
{
RetVal = false;
}
//return ThisAnimator.GetCurrentAnimatorStateInfo(0).length >
// ThisAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime;
return RetVal;
}
//============================================
public IEnumerator OpenDoorAnimationWaitTime(float t)
{
yield return new WaitForSeconds(t);
// Play AudioSource
GlassDoorOpeningSoundEffect.Play();
//==================================================
// Recently added this to the code...
GlassDoorAtStart_GO_Animator.SetBool("isOpening", true);
//==================================================
GlassDoorAtStart_GO_Animator.Play("GlassDoorAtStart_GO_Animation_OPEN");
}
//=============================================
public IEnumerator CloseDoorAnimationWaitTime(float t)
{
yield return new WaitForSeconds(t);
// Play AudioSource
GlassDoorOpeningSoundEffect.Play();
//===============================================
// Recently tried adding this next line to my code...
GlassDoorAtStart_GO_Animator.SetBool("isClosing", true);
//===============================================
GlassDoorAtStart_GO_Animator.Play("GlassDoorAtStart_GO_Animation_CLOSE");
}
//=====================================================
Your answer
Follow this Question
Related Questions
How do I setup a Shooting Trigger to make an Animation play on a Platform? 0 Answers
Trigger not playing Animation 0 Answers
Animator Button Plus Transition 0 Answers
Issues with Animator Controller - Trigger does not reset to false after being set with SetTrigger 2 Answers
Play animation from animator? 1 Answer