I need to write a script that will call up an animated file, play up to a certain frame when a character enters the trigger box and plays the rest of the animation when the character exits the trigger box.
I found this script in the forums that was written by “alexander”
var door : GameObject;
var openAnimation : AnimationClip;
var closeAnimation : AnimationClip;
function Awake() {
door.animation.AddClip(openAnimation, “Open”);
door.animation.AddClip(closeAnimation, “Close”);
}
// Opening Door
function OnTriggerEnter() {
door.animation.Play(“Open”);
}
// Closing Door
function OnTriggerExit() {
door.animation.Play(“Close”);
}
I removed the section in his script that would load up a level.
I was told I need to export the doors that would be animated into separate files and animate them. I animated a door closed at frame 0 opened at frame 2 and closed again at frame 4. So I need to tell the script to be called up when a collider (which is the character) enters the trigger box.
Each door will have it’s own trigger box to call up it’s own script.
Now to make things even trickier. How would I ensure that the doors would remain closed until the script was called up to open the door?
I’m thinking that “AnimationClip” should be replaced with “b9smlldranimated” because that’s the file I want the script to call up. That one’s obvious enough.
I am also having some trouble setting up something similar:
right now I have this code that may provide some help, it does work, but for me it doens’t realy work the way it should because simply doesn’t respond very well. I don’t know why but I am still quite new to programming interactivity with Unity.
ar deurObject:GameObject;
var check:System.Boolean;
function OnTriggerStay () {
print("Ik zit in de trigger");
if (Input.GetButtonDown ("Fire1")) {
if (check == false) {
print("Deur geopend");
deurObject.animation.Play("Open");
check = true;
} else {
print("Deur sluiten");
deurObject.animation.Play("Close");
check = false;
}
}
}
I think the OnTriggerStay function is not called every frame. That is the reason you are having problems with your script.
I suggest that you use Update funtion and OnTriggerEnter.
function OnTriggerEnter () {
canOpen=true;
}
function OnTriggerExit () {
canOpen=false;
}
And put your code inside update funtion:
function Update () {
if (Input.GetButtonDown ("Fire1") canOpen) {
deurObject.animation.Play("Open");
}
}
Thanks Alex_R
I got it to work for me. I changed the code a little to fit my specific needs.
var deurObject:GameObject;
var triggerStay:System.Boolean;
var isOpen:System.Boolean;
function OnTriggerExit() {
triggerStay= false;
}
function OnTriggerEnter() {
triggerStay = true;
}
function Update() {
print("Triggerstay= " + triggerStay);
if(triggerStay) {
if (Input.GetButtonDown ("Fire1") !isOpen) {
print("Open door");
deurObject.animation["Open"].speed = 1;
deurObject.animation.Play("Open");
isOpen = true;
} else if (Input.GetButtonDown ("Fire1") isOpen) {
print("Close door");
deurObject.animation["Open"].speed = -0.1;
deurObject.animation.Play("Open");
isOpen = false;
}
}
}