Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animation not playing in certain situation...

Discussion in 'Getting Started' started by MikeTeavee, Sep 9, 2015.

  1. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    I'm making a simple first-person-shooter. I have two scripts attached to a gun. One plays a gun-bob-animation when walking, the other plays a gun-lowering-animation while jumping. Less importantly, they both use a bool called CanPushOffGround to indicate whether your feet are touching the ground or not.

    This all works fairly well, except for one issue, I cannot get the jump-animation (JumpAnim) to animate while doing a run-jump (Holding down 'W' and hitting Spacebar).

    Here are the two scripts...

    Code (CSharp):
    1. // WalkingMovement.cs
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class WalkingMovement : MonoBehaviour
    6. {
    7.     public GameObject UsingDetector;
    8.     public bool CanPushOffGround;
    9.     // Use this for initialization
    10.     void Start () {
    11.     }
    12.     // Update is called on    ce per frame
    13.     void Update () {
    14.         CanPushOffGround = (UsingDetector.GetComponent<GroundDetector> ().FeetOnGround);
    15.  
    16.         if (Input.GetKey (KeyCode.W) && CanPushOffGround) {
    17.             gameObject.GetComponent<Animation> ().Play ("WalkAnim");
    18.         } else {
    19.             gameObject.GetComponent<Animation> ().Stop ("WalkAnim");
    20.         }
    21.     }
    22. }
    Code (CSharp):
    1. // JumpMovement.cs
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class JumpMovement : MonoBehaviour
    6. {
    7.     public GameObject UsingDetector;
    8.     public bool CanPushOffGround;
    9.     // Use this for initialization
    10.     void Start () {
    11.     }
    12.     // Update is called once per frame
    13.     void Update () {
    14.         CanPushOffGround = (UsingDetector.GetComponent<GroundDetector> ().FeetOnGround);
    15.         if (Input.GetKeyDown ("space") && CanPushOffGround) {
    16.             gameObject.GetComponent<Animation> ().Play ("JumpAnim");
    17.         }
    18.     }
    19. }
     
    Last edited: Sep 9, 2015