Friday, May 3, 2019

unity - How can i change door close/open speed in animator, and how to tell player if the door is close wait/idle until the door will open?



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

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


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.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...