Search Unity

AnimationScript help needed

Discussion in 'Scripting' started by malnar1996, Jan 3, 2011.

  1. malnar1996

    malnar1996

    Joined:
    Oct 31, 2010
    Posts:
    31
    Hello

    I have one animation problem.
    My animation is made of 4 elements : Idle, Walk, Shoot, Jump.
    I don't know if this is important to you but here is how these clips are set

    Walk - Loop
    Idle - Loop
    Shoot - Once
    Jump - Once

    I have made AnimationScript that's looking like this:

    Code (csharp):
    1. function Update () {
    2.  
    3.     if (Input.GetAxis("Walk"))
    4.        animation.CrossFade ("Walk");
    5.     else if (Input.GetAxis("Jump"))
    6.        animation.CrossFade("Jump");
    7.     else if (Input.GetAxis("Fire1"))
    8.        animation.CrossFade("Shoot");   
    9.     else
    10.       animation.CrossFade("Idle");
    11. }  
    Here are some more axis settings:
    Walk:
    Positive Button: w
    Negative Button: d
    Alt Positive Button: up
    Alt Negative Button: down

    Jump:
    Positive Button: space

    Fire1:
    Positive Button: joystick button 0



    On the "Shoot" clip, only gun is moving and on the "Walk" clip, gun is not moving (it doesn't change it's transform)
    When I hold "w" (the player walks forward) and when I press "Fire1", the player continues walking (nothing about "Walk" clip changes) and the gun doesn't move but when the "Idle" clip is playing and I press "Fire1", "Shoot" clip playes.
    I tryed writing "if" instead of "else if" but then only "Idle" clip is playing.

    Please help me.

    Thank you!!!
     
    Last edited: Jan 3, 2011
  2. ma.parizeau

    ma.parizeau

    Joined:
    Mar 21, 2010
    Posts:
    116
    Probably, you wold want to take a look at animation blending for walking and shooting at the same time.

    Is the gun attached to the player or are they two separate meshes? If they are separated, try to put them together. Otherwise, I would suggest that you take a look at the tutorial that Unity has on moving your character and shooting. You could take a look at the boot camp project that comes with Unity 3 as well.
     
  3. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Try putting the shoot animation in a higher layer than walk, in Start()... by default all animations are in layer 0, so:

    Code (csharp):
    1. Start(){
    2.     animation["shoot"].layer = 1;
    3. }
    Should help.

    But by having your if-else structure the way it is, if the player is walking, the shoot animation could never happen... because it will always be stuck in that first if block.

    Try something like:

    Code (csharp):
    1.  
    2. function Update () {
    3.  
    4.     if (Input.GetAxis("Walk"))
    5.         animation.CrossFade ("Walk");
    6.     else
    7.         animation.CrossFade("Idle");
    8.    
    9.     if (Input.GetAxis("Fire1"))
    10.         animation.CrossFade("Shoot");
    11.  
    12.      
    13. }
    14.