- Home /
Animate Doors and Button Through Script
Alright, I have tried to create doors, a button, and a collider to trigger the button in order to make it so that if a player walks onto a button, it will open the doors. However, I would like the opening and closing to be animated with script and not an actual animation because I will be using many doors throughout my game and I do not think it is possible to make an animation non-position specific. To accomplish this I have tried to use lerp. This works when it goes without being activated, but when it is activated, the doors and button do not animate, but instead instantaneously change position. The script I use on the doors and button is below and the script I use on the button activator is below that. Thanks for any help.
* Code is fixed and now animates over several frames.
var start : Transform; var end : Transform; var ObjectSpeed = 1.0; private var Down = 0; private var startTime : float;
function ButtonDown() { startTime = Time.time; Down = 1; print("button Down"); }
function ButtonUp() { Down = 2; print("button Up"); }
function Update(){
var dist = Vector3.Distance(start.position, end.position);
var elapsedTime = Time.time - startTime;
if(Down == 1){
transform.position = Vector3.Lerp(start.position, end.position,elapsedTime*ObjectSpeed);
if (dist <= 0)
Down = 0;
}
if(Down == 2){
transform.position = Vector3.Lerp(end.position, start.position, elapsedTime*ObjectSpeed);
if (dist <= 0)
Down = 0;
}
}
This is the button activator script:
var RightDoor: Transform; var LeftDoor: Transform; var MyButton: Transform;
function OnTriggerEnter (other: Collider) { if(other.gameObject.CompareTag("Player") || other.gameObject.CompareTag("Box")) { print("yes"); MyButton.gameObject.SendMessage("ButtonDown", null); LeftDoor.gameObject.SendMessage("ButtonDown", null); RightDoor.gameObject.SendMessage("ButtonDown", null); } }
function OnTriggerExit (other: Collider) {
if(other.gameObject.CompareTag("Player") || other.gameObject.CompareTag("Box"))
{ MyButton.gameObject.SendMessage("ButtonUp", null); LeftDoor.gameObject.SendMessage("ButtonUp", null); RightDoor.gameObject.SendMessage("ButtonUp", null); } }
Answer by loopyllama · Mar 09, 2011 at 10:49 AM
There are several issues going on here. Update() happens every frame. If you follow the logic: if down = 1 on, say frame 1, then the door starts to animate that frame and then door = 0. That means on frame 2 the door will not animate any longer because door = 0. You are only getting 1 frame of animation. To fix, you need to do something like this:
function Update() { if(Down == 1) { transform.position = Vector3.Lerp(start.position, end.position, Time.time*ObjectSpeed);
if (transform.position >= end.position)
Down == 0;
}
...
This says "keep animating the door until the door position is greater than or equal to my end position".
Why is your door fully animating over 1 frame? because the Lerp method takes 3 parameters: start number, end number, and a 0 to 1 that controls the blending between the start number and end number. That is right! When that number hits 1 or greater you get the full value of the end number. You input Time.time ObjectSpeed as your 0 to 1 value. Time.time is the current time which will always be greater than 1. What you want is to check for elapsed time. You need to do something like this: startTime = Time.time in your Down function, declaring var startTime : float at the top of your file. Then in your Update function do something like var elapsedTime = Time.time - startTime. Then use elapsedTime ObjectSpeed instead of Time.time * ObjectSpeed.
Makes sense?
All I had to do was compare the distance of the doors ins$$anonymous$$d of the start and end position, but now it works!
Your answer
Follow this Question
Related Questions
Trigger door animation with external trigger 1 Answer
Animation clip does not exist? 1 Answer
Door Movement Animation 3 Answers
Need help with my animation script 2 Answers
Respawn script and door animation help 0 Answers