Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Player running and attacking at the same time?

Discussion in 'Scripting' started by Jenovah, Dec 9, 2011.

  1. Jenovah

    Jenovah

    Joined:
    Dec 9, 2011
    Posts:
    14
    Player running and attacking at the same time?
    Would really like to figure out how to get a player to either run and attack at the same time, OR less ideal stop their forward movement while the attack animation is playing. Right now when I press E (attack) and still am moving forward the characters run animation stops playing, and he slides forward while playing the attack animation

    I tried checking out Unity's AnimationMix documentation HERE

    But couldn't figure out how to assign the shoulder, joints etc...

    Here's my code that I am working with:

    Code (csharp):
    1. function Start () {
    2.    // Set all animations to loop
    3.    animation.wrapMode = WrapMode.Loop;
    4.    // except shooting
    5.    animation["jump"].wrapMode = WrapMode.Once;
    6.    animation["attack1"].wrapMode = WrapMode.Once;
    7.  
    8.    // Put idle and walk into lower layers (The default layer is always 0)
    9.    // This will do two things
    10.    // - Since shoot and idle/walk are in different layers they will not affect
    11.    //   each other's playback when calling CrossFade.
    12.    // - Since shoot is in a higher layer, the animation will replace idle/walk
    13.    //   animations when faded in.
    14.    animation["jump"].layer = 1;
    15.    animation["attack1"].layer = 1;
    16.  
    17.    // Stop animations that are already playing
    18.    //(In case user forgot to disable play automatically)
    19.    animation.Stop();
    20. }
    21.  
    22. function Update () {
    23.    // Based on the key that is pressed,
    24.    // play the walk animation or the idle animation
    25.    if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.2)
    26.       animation.CrossFade("run");
    27.    else
    28.       animation.CrossFade("idle");
    29.  
    30.    // jump
    31.    if (Input.GetKeyDown(KeyCode.Space))
    32.       animation.CrossFade("jump");
    33.      
    34.    // Attack
    35.    if (Input.GetKeyDown(KeyCode.E))
    36.       animation.CrossFade("attack1");
    37.      
    38.    // Move Left
    39.    if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.2)
    40.       animation.CrossFade("run");    
    41. }
    Any helps or tips on how to approach this would be great.

    Thanks.
     
  2. Jenovah

    Jenovah

    Joined:
    Dec 9, 2011
    Posts:
    14
    Found a solution on another forum if anybody's interested:


    Code (csharp):
    1. var speed : float = 12;
    2. var gravity : float = 20.0;
    3. private var moveDirection : Vector3 = Vector3.zero;
    4.  
    5. private var shootanim : AnimationState;function Start ()
    6.  
    7. {
    8.    
    9.    rootBone = transform.Find("Bip01/Bip01 Pelvis/Bip01 Spine/Bip01 Spine1");
    10.  
    11.    shootanim = animation["attack1"];
    12.    shootanim.layer = 2;
    13.    shootanim.blendMode = AnimationBlendMode.Blend;
    14.    shootanim.wrapMode = WrapMode.ClampForever;
    15.    shootanim.enabled = false;
    16.    shootanim.weight = 1.0;
    17.    shootanim.AddMixingTransform(rootBone);
    18.    
    19.    animation["run"].layer = 1;
    20.    animation["idle"].layer = 1;
    21.    animation["run"].weight = 0.1;
    22. }
    23.  
    24. function Update () {
    25.    if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
    26.       animation.CrossFade("run");
    27.    else
    28.       animation.CrossFade("idle");
    29.  
    30.    if (Input.GetKeyDown (KeyCode.LeftShift)){
    31.       shootanim.normalizedTime = 0;
    32.       shootanim.enabled = true;
    33.     }
    34. }
     
  3. Jenovah

    Jenovah

    Joined:
    Dec 9, 2011
    Posts:
    14
    Haha Spoke too soon. He'll attack while running however afterwards he no longer swings his arms when he's running.....Bummer :(

    Here's the code, anybody got and tips to help fix that?

    Code (csharp):
    1.  
    2. var speed : float = 12;
    3. var gravity : float = 20.0;
    4. private var moveDirection : Vector3 = Vector3.zero;
    5.  
    6. private var shootanim : AnimationState;function Start ()
    7.  
    8. {
    9.    
    10.    rootBone = transform.Find("Bip01/Bip01 Pelvis/Bip01 Spine/Bip01 Spine1");
    11.  
    12.    shootanim = animation["attack1"];
    13.    shootanim.layer = 2;
    14.    shootanim.blendMode = AnimationBlendMode.Blend;
    15.    shootanim.wrapMode = WrapMode.ClampForever;
    16.    shootanim.enabled = false;
    17.    shootanim.weight = 1.0;
    18.    shootanim.AddMixingTransform(rootBone);
    19.    
    20.    animation["jump"].wrapMode = WrapMode.Once;
    21.    animation["jump"].layer = 1;
    22.    animation["run"].layer = 1;
    23.    animation["idle"].layer = 1;
    24.    animation["run"].weight = 0.1;
    25. }
    26.  
    27. function Update ()
    28.  
    29. {
    30.    if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
    31.       animation.CrossFade("run");
    32.    else
    33.       animation.CrossFade("idle");
    34.  
    35.    if (Input.GetKeyDown (KeyCode.LeftShift))
    36.    {
    37.       shootanim.normalizedTime = 0;
    38.       shootanim.enabled = true;
    39.     }
    40.    
    41.     // Move Left
    42.     if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.2)
    43.     animation.CrossFade("run");
    44.    
    45.     // jump
    46.     if (Input.GetKeyDown(KeyCode.Space))
    47.  
    48.