I know this have come up before, but none of the previous posts helped me.
So here is what im after:
I want to attach a script to my door that senses when a specific trigger has been entered by the player, eg. the on in front of it. And when the trigger have been entered by the player i want the script to call an animation that i made with Unity’s Animation program.
I dont even know how to trigger an animation.
And because im new to scripting i dont know where to start.
[SOLVED]:
I’ve solved my problem, i’ll post my scripts here if someone else needs it:
With Trigger:
var doorOpened : boolean = false;
var AnimationSource = gameObject;
var timer : float = 0.0;
var anim1 : String;
var anim2 : String;
function Update () {
if (doorOpened) {
timer += Time.deltaTime;
}
if (timer >= 5) {
shutDoor();
}
}
function OnTriggerEnter () {
doorOpened = true;
AnimationSource.animation.Play(anim1);
}
function shutDoor() {
AnimationSource.animation.Play(anim2);
doorOpened = false;
timer = 0;
}
place on the trigger, you choose on which gameObject the animations can be found. And type into Anim1 or Anim2 what the animations are called.
With ColliderHit:
var doorOpened : boolean = false;
var timer : float = 0.0;
var AnimationSource = gameObject;
var anim1 : String;
var anim2 : String;
function OnControllerColliderHit(hit:ControllerColliderHit){
if((hit.gameObject.tag == "screen") (doorOpened == false)){
openDoor();
}
}
function Update(){
if(doorOpened){
timer += Time.deltaTime;
}
if(timer >= 5){
shutDoor();
}
}
function shutDoor(){
AnimationSource.animation.Play(anim2);
doorOpened = false;
timer = 0;
}
function openDoor(){
doorOpened = true;
AnimationSource.animation.Play(anim1);
}
place on the FPS controller, and choose the source of the animations.
Not to take credit for these. They are based on the script found in the learnmesilly.com tutorials.
Did you go through the tutorial? I have to admit it was a struggle for me to use the script for my own door in my project but it does work now although there is a glitch in the imported animation.
I don’t know if there is any difference between Unitys animations and imported ones, I just can’t get Unitys animations to work with Will’s script so I guess there is some other way of scripting for them.
If you want to post your script and settings I can compare it to mine and see if I can figure out what the problem is.
i figured it out. I didnt have the normal FPS Controller found in the Standard Package, i used a custom made FPS that effects rigidbodies, so it wasnt the controller that the the IF statement was looking for.
I actually managed to make his example play with my own animations made from within Unity. Now my only question is, how can i make it look for an hit with another gameobject, for example i want to take his exact script but open when a keycard hit a screen and not a Controller.
I hope i make some sense
And is there som way i could make the script a bit more universal, so i dont need 1 script for every single event? like call in a gameObject etc?
How did you get it to work with the Unity animations? What did you do?
Not sure quite what you mean but I guess you could try adding the script to objects other than the controller and naming items other than the door in the script so the animation is triggered by a different set of collisions. It’s on collision play animation, I guess you can specify a different collision.
I’ve a few things I can adapt it to if I can get it to work with Unity animations.
it doesnt need to call for idle, i just have 2, dooropen and doorclose.
quick question, how can i control what stands inside the animation.Play("text") //i wanna control what stands in "text" from outside the script (like var TEXT = gameObject; but so i can typ in what text it should contain) maybe by typing it in inside the Inspector so that i can use this script all over?
That’s what I did and the sound plays but no animation works. It’s really frustrating as I can’t figure out why.
I don’t know how you can control elements of that script from another but I know it can be done. Hope someone jumps in here and answers more helpfully.
i had a problem in the beginning, my animations werent saved, so when my animations didnt trigger i looked to see if there was something wronf with the animation in question. Just play the animation just to be sure.