- Home /
Why the speed parameter in Animator change the animation speed only for door open but when the door is closing the speed still very fast ?
The code:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Animator))]
public class DoorActivatorMecanim : MonoBehaviour
{
public float doorActivateSpeed = 1f;
private Animator DoorAnimator;
void Start()
{
DoorAnimator = GetComponent<Animator> ();
}
void OnTriggerEnter(Collider col)
{
DoorAnimator.SetFloat("speed", doorActivateSpeed);
DoorAnimator.Play("Door Open");
}
private void OnTriggerExit(Collider other)
{
DoorAnimator.Play("Door Close");
}
}
Before i tried to put the line:
DoorAnimator.SetFloat("speed", doorActivateSpeed);
In Update
But also in Update or as it is now it's only effecting the Door Open and the Door Close is not. When it's getting to the door close the door sut down quick like falling down and not animating down like when it open. Can't figure out why it's not effecting also the door close.
This is a screenshot showing the door with inspector:
And this screenshot showing the animator with the speed parameter and on the right the animations of the door open and door close settings they are both the same: Length start at 0 to 50 and all settings are the same.
And in the door open and door close states both are the same and in both i checked the box near Multiplier of parameter and selected the speed.
But it's only working on the Door Open and not the Door Close.
Here is a small video clip i recorded showing what i mean when the door is open slowly very slow i set it to 0.1f and then when the door is close it shut down like falling down and not closing slowly.
Answer by shadowpuppet · Nov 26, 2017 at 05:19 PM
Your default door state is door close, I don't know if that is an actual animation of the door closing or just a single frame of it in the closed state, but that is the default so after the door open animation plays the animator goes directly to that - no transition. I mean I do things differently but I would have a default state of the door closed ( no animation or a 2 frame loop of it closed or something) then OnTriggerEnter instead of "play" I would have done a SetTrigger to open the door and on exit frame of that animation have it go to an a state of the door open. From there make another trigger transition to make the door close OnTriggerExit. This requires an animator controller ( can't tell if there is one). I see you call for one in the script but just make one and actually give the Door the Animator component so you can give it trigger transitions
![using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Animator))]
public class DoorActivatorMecanim : MonoBehaviour
{
public float doorActivateSpeed = 1f;
private Animator DoorAnimator;
void Start()
{
DoorAnimator = GetComponent<Animator> ();
}
void OnTriggerEnter(Collider col)
{
DoorAnimator.SetFloat("speed", doorActivateSpeed);
DoorAnimator.SetTrigger ("openDoor");
}
private void OnTriggerExit(Collider other)
{
DoorAnimator.SetTrigger ("closeDoor");
}
}][1]
[1]: /storage/temp/106273-graph.jpg