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. Dismiss Notice

Attack animation with the mouse

Discussion in 'Scripting' started by cruising, Jun 23, 2014.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Hello!

    I have tried a lot of diff types of codes, but i cant get it to work at all.
    The thing is that i trying to make so you only have to click on the mouse button once and never again to play the fully animated attack animation.

    My problem atm is that i can only make so you can to hold down the mouse button to play the animation fully, and if i try some other code(s) i have to click like 10 times fast to play the whole animation in a laggy state.

    I think it has to do with the
    Code (JavaScript):
    1. if (Input.GetMouseButton(0))
    ?
    So i have to ask you guys here for help :)

    here the code for hold down button attack.
    Code (JavaScript):
    1.     function Update ()
    2.     {
    3.        if (Input.GetMouseButton(0))
    4.    
    5.         {
    6.            animation.Play("attack1");
    7.    
    8.         }
    9.            else if (Input.GetMouseButton(1))
    10.         {
    11.            animation.Play("attack2");
    12.    
    13.         }        
    14.    
    15.     }
    16.  
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    You should watch the stealth tutorial. It has everything you need to get started with Mechanim.
     
  3. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    I dont have problems with the actual animations, just to make them play once and to the end, and not canceling the animations if i release the mouse button.
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    That is exactly what the tutorial teaches you.
     
  5. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
  6. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Then you are doing something wrong, I am afraid. Animation parameters work fine if you implement them correctly.
     
  7. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    I have no idea what/if im doing wrong here. And exactly nothing happens when i press fire.

    Here is what i have atm from that link
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Attack : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     protected Animator animator;
    8.    
    9.     public float DirectionDampTime = .25f;
    10.    
    11.     void Start ()
    12.     {
    13.         animator = GetComponent<Animator>();
    14.     }
    15.    
    16.     void Update ()
    17.     {
    18.         if(animator)
    19.         {
    20.             //get the current state
    21.             AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
    22.            
    23.             //if we're in "Run" mode, respond to input for jump, and set the Jump parameter accordingly.
    24.             if(stateInfo.nameHash == Animator.StringToHash("Base Layer.RunBT"))
    25.             {
    26.                 if(Input.GetButton("Fire1"))
    27.                     animator.SetBool("attack1", true );
    28.             }
    29.             else
    30.             {
    31.                 animator.SetBool("attack1", false);              
    32.             }
    33.            
    34.             float h = Input.GetAxis("Horizontal");
    35.             float v = Input.GetAxis("Vertical");
    36.            
    37.             //set event parameters based on user input
    38.             animator.SetFloat("Speed", h*h+v*v);
    39.             animator.SetFloat("Direction", h, DirectionDampTime, Time.deltaTime);
    40.         }      
    41.     }      
    42. }
     
  8. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Did you set up the controller properly?
     
  9. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    If the controller you say is the GetComponent<Animator, then yes. or else the other codes would not even work for animations. And the only settings i get from the actual code is "Direction Damp Time" in Inspector