Search Unity

Cant stop attack animation once clicking button.

Discussion in 'Animation' started by Helel, Oct 25, 2013.

  1. Helel

    Helel

    Joined:
    Oct 25, 2013
    Posts:
    3
    I dont know what is going on here. It seems like a pretty simple problem but I cant figure it out myself. I set my char to attack when pressing left mouse button, somehow it keeps non-stopped attacking. Can anybody help me :( It already took me half a day. Here's my script. Thank you in advance :(

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class KnightController : MonoBehaviour {
    5.    
    6.     public float speed = 10;
    7.     public float rotationSpeed = 30;
    8.     public float animSpeed = 1.5f;
    9.    
    10.     private GameObject player;
    11.     private Animator anim;
    12.     private AnimatorStateInfo currentBaseState;
    13.     private HashIDs hash;
    14.     private float cdLeftAtk = 1;
    15.     private float timeReset = 0;
    16.    
    17.     void Awake () {
    18.         player = GameObject.FindGameObjectWithTag(Tags.player);
    19.         anim = player.GetComponent<Animator>();
    20.         anim.speed = animSpeed;
    21.         hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
    22.         currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
    23.     }
    24.    
    25.     void FixedUpdate () {
    26.         float h = Input.GetAxis("Horizontal");
    27.         float v = Input.GetAxis("Vertical");
    28.        
    29.         anim.SetFloat("speed", v);
    30.         transform.Translate(0,0, v * speed * Time.deltaTime);
    31.         transform.Rotate(0, h * rotationSpeed * Time.deltaTime, 0);
    32.         if(timeReset > 0)
    33.             timeReset -= Time.deltaTime;
    34.         else if (timeReset < 0)
    35.             timeReset = 0;
    36.        
    37.         if(Input.GetMouseButtonUp(0))
    38.         {
    39.             Debug.Log(timeReset);
    40.             if(timeReset == 0)
    41.             {
    42.                 anim.SetBool("LeftClick", true);
    43.                 timeReset = cdLeftAtk;
    44.                 if(currentBaseState.nameHash != hash.leftAttackState)
    45.                 {
    46.                     anim.SetFloat("attackparameter", Random.Range(1.0f, 4.0f));
    47.                 }
    48.                 else
    49.                 {
    50.                     anim.SetBool("LeftClick", false);
    51.                        
    52.                 }
    53.  
    54.             }
    55.  
    56.         }
    57.     }
    58. }
    59.  
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    11,588
    Code (csharp):
    1.  
    2.  void FixedUpdate () {
    3.     ...
    4.     if(Input.GetMouseButtonUp(0)) {
    5.  
    Input.GetMouseButtonUp(0) is only going to be true once, when you unclick the mouse. So you'll never get to anim.SetBool("LeftClick", false);

    Perhaps you could use something like:

    Code (csharp):
    1. anim.SetBool("LeftClick", Input.GetMouseButton(0));
    This guarantees that the LeftClick parameter is always true when the mouse button is down, and always false when the mouse button is up.
     
    theANMATOR2b likes this.
  3. Helel

    Helel

    Joined:
    Oct 25, 2013
    Posts:
    3
    Much appreciate this :D though there're still somes minor bugs but it helps me find a way. Tks again :)
     
  4. Archania

    Archania

    Joined:
    Aug 27, 2010
    Posts:
    1,662
    theANMATOR2b likes this.
  5. Helel

    Helel

    Joined:
    Oct 25, 2013
    Posts:
    3
    I'll try it. Thank you :)
     
unityunity