Search Unity

Need help with my weapon script (JS)

Discussion in 'Scripting' started by General Jackson, Jan 20, 2010.

  1. General Jackson

    General Jackson

    Joined:
    Oct 31, 2009
    Posts:
    73
    Hi, I am making a gun script for my game and here is my code.
    It sorta works and it sorta doesnt
    Code (csharp):
    1.  
    2. function Update () {
    3. animation.Play ("walk");
    4.  
    5. if (Input.GetButton("r")) {
    6. animation.Play("reload loop");
    7.    
    8.    }
    9. }
    It is supposed to play the reload animation when the player presse the r key. Instead it plays the first frame then stops.
    What is wrong with it?
     
  2. General Jackson

    General Jackson

    Joined:
    Oct 31, 2009
    Posts:
    73
    Bump.

    Anyone?
     
  3. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    If you're not calling the stop, then the animation is probably only one frame long. I'd make sure the animation plays properly by putting a version of the model into the scene and telling it to play the reload automatically on game start. If it freezes at the animation start again, the animation's the problem. If not, then something is calling animation.Stop().
     
  4. General Jackson

    General Jackson

    Joined:
    Oct 31, 2009
    Posts:
    73
    It works perfect if I set it as the animation at the start of the script, but I must be getting something wrong:(
    Guess I'll check the FPS tut.
     
  5. bpatters

    bpatters

    Joined:
    Oct 5, 2009
    Posts:
    164
    The reason is that the next frame you are telling it to play the walk animation which is overruling the reload animation.

    add this:
    Code (csharp):
    1.  
    2. function Start() {
    3.    animation["reload loop"].layer =2;
    4. }
    5. function Update () {
    6.    animation.Play ("walk");
    7.  
    8.    if (Input.GetButton("r")) {
    9.       animation.Play("reload loop");
    10.    }
    11. }
    12.