- Home /
Pause animation door when you block it while opens/closes
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.
Two ways to do this: 1. Add an animation event at the start of the opening/closing animation, and make it set a bool "transition" to true in the door script. Similarly, add another event at the end and amke it set the bool to false. Then, before stopping the door animation (by setting the animation speed to 0 or whatever), you first check if the door is transitioning 2. Similar to the above, but ins$$anonymous$$d of using a bool and events, check if the door's current anim state is opening or closing, and only then make it stop.
i have already done that, but the problem is another i want that door stops ONLY IF the player stay towards the way of the door
Well, can't you change the trigger shape and position then?
Answer by Vollmondum · Jul 20, 2016 at 08:51 PM
You don't need animations etc. Unity people did everything for you.
Check this out:
Answer by bromley · Jul 20, 2016 at 06:43 PM
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.
$$anonymous$$AYBE SOLVED, look at this script
using UnityEngine;
using System.Collections;
public class BlockDoor : $$anonymous$$onoBehaviour {
private Animation anim;
private Collider col;
private GameObject door;
private Rotating_Normal_Door doorScript;
void Start ()
{
door = transform.parent.gameObject;
anim = door.GetComponent<Animation> ();
doorScript = door.GetComponent<Rotating_Normal_Door> ();
col = GetComponent<Collider> ();
}
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;
}
}
}
void Update()
{
if (doorScript.open == true)
{
transform.localPosition = new Vector3 (-1.0F, 0.0F, 0.0F);
}
if (doorScript.open == false)
{
transform.localPosition = new Vector3 (-1.0F, 0.2F, 0.0F);
}
}
}
Your answer
Follow this Question
Related Questions
Open Door Via Trigger Collider 0 Answers
play a door animation with collider 1 Answer
Within sphere collider to click on door that opens 0 Answers
Animating Colliders? 2 Answers
Triggering door animation with collider 2 Answers