x


Make the animation play at the start of the time waited, not the end of it?

I'm working on an FPS gun script, and i took the "reload" function from the script in the FPS tutotrial.

function Reload () {

    yield WaitForSeconds(reloadTime);

    if (clips > 0) {
        clips--;
        bulletsLeft = bulletsPerClip;
    }
}

I added animation.Play like this:

function Reload () {

    yield WaitForSeconds(reloadTime);

    if (clips > 0) {
        clips--;
        bulletsLeft = bulletsPerClip;
        animation.Play ("Reload");    // Line added.
    }
}

The problem is that it plays the animation after the "WaitForSeconds". What should I do to make the animation play within the time span?

more ▼

asked Dec 17 '10 at 12:36 AM

user-3061 (yahoo) gravatar image

user-3061 (yahoo)
126 29 29 37

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

1 answer: sort voted first

Move the wait to after the animation starts. You probably also want to have it inside the clips test, and update bullets left after the wait is over.

function Reload () {    
    if (clips > 0) {
        clips--;
        animation.Play ("Reload");
        yield WaitForSeconds(reloadTime);
        bulletsLeft = bulletsPerClip;
    }
}

If it would help you understand the flow of code, let's break down the Reload function into smaller parts:

function Reload ()
{
    if (clips > 0) 
    {
        BeginReload();
        yield WaitForSeconds(reloadTime);
        EndReload();
    }
}

function BeginReload () 
{
    clips--;
    animation.Play("Reload");
}

function EndReload () 
{
    bulletsLeft = bulletsPerClip;
}
more ▼

answered Dec 17 '10 at 12:55 AM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

actually i tried that before,but the scripts still wont work.. the waiting part functions correctly, but it wont play the animation...

Dec 17 '10 at 02:36 AM user-3061 (yahoo)

But you wrote: "The problem is that it plays the animation after the WaitForSeconds". Make sure the object has an animation and the animation has a clip called "Reload".

Dec 17 '10 at 02:43 AM Statement ♦♦

yes it has a clip named reload, im very sure

Dec 17 '10 at 02:54 AM user-3061 (yahoo)

Okay, i got it working now (partially), but your answer is correct, it was some other animation's problem. Thanks!

Dec 17 '10 at 03:21 AM user-3061 (yahoo)
(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:

x3807
x3465
x3343
x170
x85

asked: Dec 17 '10 at 12:36 AM

Seen: 896 times

Last Updated: Dec 17 '10 at 01:40 AM