I created a door with 3 animations: Idle, Open and Close animation. When I interact with the door, it opens/closes, and if I do it while block its way, it pushes me.
Then I want to create a script that pauses door animations when the player blockes it… and I can do it!
The problem is now: I want to create a script that pauses door animations ONLY IF the player hinds the opening/closing of the door. With the actual script, where I use OnTriggerEnter, if player touches the door, doesn’t matter where it touches, the door animation pauses. But I want this only if the player touches the door towards the opening/closing.
I thought to use Collider.Raycast (because the door has a box collider), but i don’t know how to distinguish the collisions from front or back of the door.
You don’t need animations etc. Unity people did everything for you.
Check this out:
Now i created this script attached in a gameobject with a box collider, and this gameobject is child of the door
using UnityEngine;
using System.Collections;
public class BlockDoor : MonoBehaviour {
private Animation anim;
private GameObject door;
private Rotating_Normal_Door doorScript;
void Start ()
{
door = transform.parent.gameObject;
anim = door.GetComponent<Animation> ();
doorScript = door.GetComponent<Rotating_Normal_Door> ();
}
void OnTriggerStay (Collider col)
{
if (col.gameObject.tag == "Player" && anim.isPlaying == true)
{
foreach (AnimationState state in anim)
{
state.speed = 0.0F;
}
}
}
void OnTriggerExit (Collider col)
{
if (col.gameObject.tag == "Player" && anim.isPlaying == true)
{
foreach (AnimationState state in anim)
{
state.speed = 1.0F;
}
}
}
}
Works good but the door stops also when player touches it on the opposite direction.