Search Unity

Animation Only playing once

Discussion in 'Animation' started by Deneth, Aug 5, 2019.

  1. Deneth

    Deneth

    Joined:
    Jun 26, 2019
    Posts:
    6
    I'm quite new to Unity and I have this code to play the attack animation when mouse is clicked, but it only happens once. For the first time it happens nicely, but the second time i press the mouse button nothing happens.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Animate : MonoBehaviour {

    public Animator anim;
    private bool attacked;

    // Use this for initialization
    void Start () {
    anim.Play("Idle");
    }

    // Update is called once per frame
    void Update () {
    if(Input.GetKey(KeyCode.Mouse0)){
    attacked= true;
    }

    if(attacked){
    anim.Play ("Attack");
    attacked=false;
    }




    }



    }
     
  2. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,256
    Hey @Deneth,
    Try changing your Play line to -
    Code (CSharp):
    1. anim.CrossFadeInFixedTime("Attack", 0);
    Also, maybe use Input.GetKeyDown, so it only fires once when the mouse is pressed.

    P.
     
  3. Deneth

    Deneth

    Joined:
    Jun 26, 2019
    Posts:
    6
    Thank you. It worked.
     
    petey likes this.