- Home /
Adjacent Elevator is activated.
I have two elevators next to each other running the same script. They are very close to each other and this can't be changed in the level.
The problem is I can open one of the elevators fine but if I'm too close to the adjacent one when doing this or if I'm inside one elevator and press E I open the other one.
This is the layout of my elevators:
The elevator on the right opens when I press E when inside or too far to the right of the elevator on the left but not the other way around. This is the script I'm using:
using UnityEngine;
using System;
using System.Collections;
public class Elevator : MonoBehaviour {
private float maxDistance = 5.5f;
public Transform elevator;
public bool open = false;
public bool verbose = false;
public void Update() {
GameObject player = GameObject.FindGameObjectWithTag("Player");
float distance = Vector3.Distance(elevator.transform.position, player.transform.position);
if(verbose) Debug.Log("Distance : " + distance);
if(Input.GetKeyDown("e") && distance < maxDistance && !open) {
elevator.animation["Default Take"].speed = 1;
elevator.animation.Play("Default Take");
open = true;
if(verbose) Debug.Log("Elevator Opening");
} else if(open && distance > maxDistance && !elevator.animation.IsPlaying("Default Take")) {
elevator.animation["Default Take"].speed = -1;
elevator.animation["Default Take"].time = elevator.animation["Default Take"].length;
elevator.animation.Play("Default Take");
open = false;
if(verbose) Debug.Log("Elevator Closing");
}
}
public void Start() {
}
}
Answer by ThePunisher · Nov 08, 2013 at 08:10 PM
It is quite possible that one of the elevator's scripts is referencing the wrong transform on the public inspector property. Can you verify this is not the case?
Edit: I know why it's able to open the other door. It's all in your if condition. You're condition checks if the the 'E' key has been hit. This condition will pass for both elevators. Then it checks if the distance from the player to the elevator is less than some threshold (5.5 it seems). Obviously, when you are close enough to both elevators both conditions will pass and both elevators would open up. Check out the approach below.
Instead of calculating the distance to the elevator to determine if I should open the door I would use a box collider placed right in front of the elevator door. If you check the isTrigger checkbox then it will call a function named OnTriggerEnter (just like Awake and Start) on every script attached to the GameObject containing this collider. From there you can simply trigger the logic for opening the door. So long as the box colliders don't overlap and the character is not wide enough to inhabit both colliders at any given time, it will only open one elevator.
Here are some more links to help picture what I'm talking about. http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html http://docs.unity3d.com/Documentation/ScriptReference/Collider-isTrigger.html
I can verify that the elevator Transforms are correct.
I have transitioned over to using BoxColliders now, thanks for your help.