x


Stop animation after playing once

I'm using RaycastHit to script a door opening animation when my character comes within a few feet of the door. How do I get the animation (of door sliding out of the way) to play only one time.

Right now, once I'm within the specified distance, the door animation keeps playing over and over (so keeps opening and closing until I move back out of distance).

Animation simply has door keyed to normal position at 0 frames, and then position.y made negative so door slides into ground at 30 frames. And here's the script I'm using:

var rayCastLength = 10;

function Update ()

{ var hit:RaycastHit;

//check if we're colliding
if(Physics.Raycast(transform.position, transform.forward, hit, rayCastLength))
{
//with a door - need to add the 'collider' in this code since using Raycast instead of just collision
if(hit.collider.gameObject.tag == "door")
    {
    //open the door
    hit.collider.gameObject.animation.Play("doorOpen");
    }

}

}

more ▼

asked Dec 20 '10 at 03:58 PM

Rob 4 gravatar image

Rob 4
113 11 12 21

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Setting ClampForever wrapMode on your door animation should do the trick.

more ▼

answered Dec 20 '10 at 04:10 PM

Paulius Liekis gravatar image

Paulius Liekis
7.3k 16 24 46

Thanks. That fixed it. So did play once and even default. I must have had it set on Loop inadvertently.

On a related note, if I want the animation to reverse if I move out of range (so door only stays open when I walk near it), is there a simple way to do that? Or a way to have the door stay open for 5 seconds and then close?

Thanks

Dec 20 '10 at 05:32 PM Rob 4

OK - wasn't just my imagination - there seems to be a glitch. If I set the doorOpen animation on Once, it repeats itself many times over a short time period - so the door races up and down about 5 to fifteen times - then after going through all those cycles, the door remains fixed in the open position.

(With clampForever this behavior didn't happen. And with Once, it seems inconsistent whether or not it Loops several times or works properly from the outset).

Is this expected behavior or some bug in the system (or in my noobie code more likely)

Dec 20 '10 at 05:43 PM Rob 4

The Once behavior might be by design - once "Once" is done it resets time to 0, so if you call Play again, it will play again. ClampForever will make animation get stuck in last frame. You can reverse playbakc by setting animation speed to negative, although I would suspect that there might be some bugs related to that in Unity 3.1 (I fixed some of those in 3.2).

Dec 20 '10 at 08:41 PM Paulius Liekis

Ok. Thanks for the clarification. Now that you've described how it works, I think I should be able to add some simple javascript to get the behavior I desire.

Dec 21 '10 at 04:44 AM Rob 4

Ok. Tried adding some code in javascript to stop the door from opening and closing repeatedly but didn't have any luck. Basically assigned a variable that would be changed after the door first opened, and tried to use that variable to stop the animation from being played again - but not working for some reason. I posted the relevant code snippet below in hopes someone could tell me where I messed up. Don't know if this is due to mistake on my part or if Unity scripting has to follow certain rules I'm not aware of.

Thanks - code in next post as running out of room:

Dec 21 '10 at 08:00 AM Rob 4
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3806
x63
x38
x5

asked: Dec 20 '10 at 03:58 PM

Seen: 3042 times

Last Updated: Dec 20 '10 at 03:58 PM