Sliding Door OnButtonDown

Hello. I’ve got a little problem. I’m trying to make two wing door, sliding horizontal when I press “e” button. Problem is that I can’t make it happend because for some reason Mathf.Lerp don’t work for me with if(Input…); I have a script, that works OnTriggerEnter. Can Anyone help me make this work with OnButtonDown??


var doorL : Transform;
var doorR : Transform;

//Initial positions for doors
private var initialDoorL : Vector3;
private var initialDoorR : Vector3;

//Control Variables
var speed = 2.0;
var openDistance = 2.0;

//Internal... stuff
private var point = 0.0;
private var opening = false;

//Record initial positions
function Start ()
	initialDoorL = doorL.localPosition;
	initialDoorR = doorR.localPosition;

//Something approaching? open doors
function OnTriggerEnter (other : Collider)
	if(other.tag == "Player")
		opening = true;

//Something left? close doors
function OnTriggerExit (other : Collider)
	if(other.tag == "Player")
		opening = true;

//Open or close doors
function Update ()
		point = Mathf.Lerp(point,1.0,Time.deltaTime * speed);
		point = Mathf.Lerp(point,0.0,Time.deltaTime * speed);
	doorL.localPosition = initialDoorL + (-Vector3.right * point * openDistance);
	doorR.localPosition = initialDoorR + (Vector3.right * point * openDistance);


Maybe add a switch/button that opens the doors somewhere? Then using raycast…

if(switch is activated)

and so on

Maybe you can put a trigger collider near to the door and then just play an animation when the ‘e’ key is pressed.

My JavaScript is rusty, but try this:

var openDoor = false;
if(Input.GetButtonDown(KeyCode.E))    openDoor = true;

if(opening && openDoor)
         point = Mathf.Lerp(point,1.0,Time.deltaTime * speed);

I’m assuming the issue is that the door doesn’t open when E is pressed. GetButtonDown is only true on the change from up to down, so you need a bool to keep that value true. You can reset the bool once the door reaches the desired open position. Alternatively, you can use GetButton; in which case the button must be held down for the door to keep transitioning.

I may be repeating what you already know, but have you tried using OnKeyDown set opening = true and OnKeyUp set opening = false?