Hi
we’ve got the animation of a door opening - with the following clips -
idle 1- 1 (looped)
open 2-44 (not looped)
close 46 - 90 (looped)
isOpen 46-46 (looped)
we’re trying to simply set up something which will open it when we approach it with an FPS controller. And are having trouble with the animation. Currently it opens when we approach it, but it slams shut straight after, seemingly sticking on frame 2. We’ve got the following script -
function OnControllerColliderHit(hit : ControllerColliderHit){
var door1 = GameObject.FindWithTag("door1");
var length1 = door1.animation["open"].length;
var currentTime1 = door1.animation["open"].time;
if ((hit.collider.gameObject.tag == "door1trigger") (currentTime1 <= length1)){
door1.animation.Play("open");
}
else if(currentTime1 >= length1){
door1.animation.Play("isOpen");
}
}
IS this a good way of doing it or not? I’ve tried simply comparing total time with current time as you can see, but is there a simpler way, like saying, when a certain clip is over, do X?
Any Help mucho appreciated,
Cheers,
Will
function OnControllerColliderHit(hit : ControllerColliderHit){
if (hit.collider.gameObject.tag != "door1trigger")
return;
var door1 = GameObject.FindWithTag("door1");
if (!door1.animation.IsPlaying("open")){
door1.animation.Play("open");
door1.animation.PlayQueued("isOpen");
}
}
Why are you using oncontrollercollider hit. It seems you want to use triggers.
Thought I had to use that because this is attached to the FPS controller. should i be using OnTriggerEnter??
…doesnt work with the Character Controller Collider does it? Tried it and I’m getting no response at all now…
thoughts?
I think what he is talking about is positioning a trigger around the door that has a script attached with a OnTriggerEnter function.
Theres already a trigger around the door… but what would the OnTriggerEnter be checking for a collision with? the player?
You put a script with OnTriggerEnter (col : Collider) on the trigger object.
That is, a Collider with isTrigger enabled.
You attach a script to that collider:
function OnTriggerEnter (col : Collider)
{
if (col.name == "Player")
{
print("Entering trigger");
}
}
Now whenever the character controller walks into the trigger you get a call OnTriggerEnter, you check if it is the player that walked into it. Then do your animation playing whatever you want to do.