1
\$\begingroup\$

In the Animator window of the door i have two states: Door Open and Door Close. And two parameters type of trigger: open and close.

In the two states Door Open and Door Close between them there are two transitions. I didn't make any Has Exit Time but i did add in both transitions the triggers as conditions. On the Door Open to Door Close i added the close condition and on the Door Close to Door Open the open condition.

door animator states

And the script that attached to the door:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Animator))]
public class DoorActivatorMecanim : MonoBehaviour 
{
    private Animator DoorAnimator;

    void Start()
    {
        DoorAnimator = GetComponent<Animator> ();
    }

    void OnTriggerEnter(Collider col)
    {
        DoorAnimator.SetTrigger("open");
    }

    private void OnTriggerExit(Collider other)
    {
        DoorAnimator.SetTrigger("close");
    }
}

The door have a box collider component.

Door

Now when the player is moving to the door when he get inside the box collider area the OnTriggerEnter is on and the door is open and stay opened until the player is moving out of the box collider area then it's getting to the OnTriggerExit and the door is close.

What i want to do is to control the door open/close speed. When the door open it open in medium speed but when the door is close it's shutting down closing at once very fast like the door is falling and not closing animation.

So i want to add another parameter in the Animator maybe type float and call it speed and somehow to control the close/open speed from the script. So i added the speed float parameter but not sure how to use it in the script and control the close/open door speed:

Door animator with speed parameter

And the second problem is if for example the player is running to the door and door open speed is slower then the player speed i want somehow to tell the player that the door is still close so idle until the door is opened when the door is opened then change from idle to walk/run again. But i'm not sure how to detect when the door is close/open and then how to tell it to the player.

Update the script with the speed parameter setting:

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");
    }
}

For some reason and i can't explain yet why the speed parameter is working only on the door open making it open slow but the door close is still very fast too fast the door is like falling/shutting down. And both open door and close door states and animation settings are the same in the editor.

Can't figure it out.

\$\endgroup\$
  • \$\begingroup\$ To notify the player when the door is closed/open you can just add a boolean inside the animator. You set your transitions(the arrows between the animation states) to "has exit time" so that it waits until the animation has ended. Then before returning to the default state you set the boolean. It should look like this: Open=>isClosed(the boolean)=>Closed \$\endgroup\$ – user100681 Nov 26 '17 at 8:50
1
\$\begingroup\$

I'd just add two new states: opening and closing. Both can transition to the open or closed state once they're done. This also avoids duplicate code if you want to add a locked state (which also transits to the opening state.

\$\endgroup\$
  • \$\begingroup\$ I used transitions before still i can't explain why the speed parameter is working on the door open animation but not on the close door. If in the script i set the speed to 0.1f it will slow down the door open but the door close will still shut down very fast like falling and not closing animation. \$\endgroup\$ – jdoedoe doedoe Nov 26 '17 at 15:48
  • \$\begingroup\$ @jdoedoedoedoe Lack of experience with Unity here, but if you repeat the same steps/settings, the results should be identical. \$\endgroup\$ – Mario Nov 26 '17 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.