- Home /
Opening one door in a scene causes all others to open
Hi, sorry I'm really new to this and I was wondering if it was possible to open more than one door using the same animation controller. At the moment I'm using a script that plays the animations when a player presses 'E' while looking at the door but when the door opens all the other doors in the scene open as well.
Here's the script I'm using:
// Use this for initialization
void Start () {
DoorAnim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
RaycastHit Hit;
if (Physics.Raycast (Player.position, Player.TransformDirection (Vector3.forward), out Hit, 5, DoorLayer)) {
print("working");
Door = Hit.transform;
if (Hit.transform.localRotation.eulerAngles.y < 45) {
Open = false;
} else
Open = true;
if (Input.GetKeyDown (KeyCode.E)) {
Open = !Open;
if (Open == true){
DoorAnim.SetInteger ("DoorState", 1);
}
else
DoorAnim.SetInteger ("DoorState", 2);
}
}
}
If anyone can help me please let me know. Thanks
Answer by Hellium · Jun 06, 2019 at 09:16 PM
I believe this script is attached to all the doors?
If so, you need to check whether the player is near the door before opening / closing it. Either use an additional trigger + OnTriggerEnter
, or do a simple distance check
public float TriggerRadius = 5; // Change this value according to your needs
// Use this for initialization
void Start () {
DoorAnim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
RaycastHit Hit;
if ( (Player.position - transform.position).sqrMagnitude < TriggerRadius * TriggerRadius && Physics.Raycast (Player.position, Player.TransformDirection (Vector3.forward), out Hit, 5, DoorLayer)) {
print("working");
Door = Hit.transform;
if (Hit.transform.localRotation.eulerAngles.y < 45) {
Open = false;
} else
Open = true;
if (Input.GetKeyDown (KeyCode.E)) {
Open = !Open;
if (Open == true){
DoorAnim.SetInteger ("DoorState", 1);
}
else
DoorAnim.SetInteger ("DoorState", 2);
}
}
Otherwise, you could attach this script to only one gameObject (your player for instance) and change it to:
public float TriggerRadius = 5; // Change this value according to your needs
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update () {
RaycastHit hit;
if ( Physics.Raycast (Player.position, Player.forward, out hit, 5, DoorLayer))
{
Transform door = hit.transform;
Animator doorAnimator = door.GetComponent<Animator>();
if( doorAnimator != null )
{
bool open = doorAnimator.GetInteger( "DoorState" ) == 1;
doorAnimator.SetInteger( "DoorState", open ? 2 : 1 );
}
}
}
Your answer
Follow this Question
Related Questions
Animations out of sync? 0 Answers
How can I change my AnimatorState instantly? 1 Answer
Animation Running Game 0 Answers
Help with animator controllers 1 Answer
why I have to anim.getComponent in update() function when I had done in Start () function 2 Answers