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

Mecanim Stop Animation

Discussion in 'Scripting' started by Paykoman, Sep 17, 2015.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys im using mecanim system for my click to move game, the move and idle part are working properly but now i implement the attack animation, but wt going on is when i press attack the animiation never stop hitting enemy, and if i deactivate loop in animation it just hit one time and stop, but the damage works anyway when i click to attack... can anyone help plz?

    I hv a bool for IsAttacking and the following script.



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Player : HumanoidControl
    6. {
    7.     private static Player instance;
    8.  
    9.     public static Player Instance
    10.     {
    11.         get
    12.         {
    13.             if(instance == null)
    14.             {
    15.                 instance = GameObject.FindObjectOfType<Player>();
    16.             }
    17.             return Player.instance;
    18.         }
    19.     }
    20.  
    21.     public Image healthBar;
    22.  
    23.     public Text healthtext;
    24.  
    25.     public static Transform opponent;
    26.  
    27.     public static Player player;
    28.  
    29.     bool block;
    30.  
    31.     private Animator anim;
    32.    
    33.     private bool attacking;
    34.  
    35.  
    36.     //character base status
    37.     public int baseIntellect;
    38.     public int baseAgility;
    39.     public int baseStrength;
    40.     public int baseStamina;
    41.    
    42.     //character modified status with equipment
    43.     private int intellect;
    44.     private int agility;
    45.     private int strength;
    46.     private int stamina;
    47.  
    48.     public Text statsText;
    49.  
    50.     void Awake()
    51.     {
    52.         player = this;
    53.  
    54.         anim = GetComponent<Animator>();
    55.     }
    56.  
    57.  
    58.     void Start()
    59.     {
    60.         curHealth = maxHealth;
    61.  
    62.         SetStatus (0, 0, 0, 0);
    63.     }
    64.  
    65.  
    66.     void Update()
    67.     {
    68.         healthBar.fillAmount = curHealth / maxHealth;
    69.  
    70.         healthtext.text = "Health: " + curHealth + "/" + maxHealth;
    71.  
    72.         Attack();
    73.     }
    74.  
    75.  
    76.     public void SetStatus(int intellect, int agility, int strength, int stamina)
    77.     {
    78.         this.intellect = intellect + baseIntellect;
    79.         this.agility = agility + baseAgility;
    80.         this.strength = strength + baseStrength;
    81.         this.stamina = stamina + baseStamina;
    82.  
    83.         statsText.text = string.Format ("Intellect: {0}\nAgility: {1}\nStrength: {2}\nStamina: {3}", this.intellect, this.agility, this.strength, this.stamina);
    84.     }
    85.  
    86.  
    87.  
    88.     void OnTriggerStay(Collider other)
    89.     {
    90.         /*if (other.name == "Enemy" && curHealth > 0)
    91.         {
    92.             curHealth -= 10;
    93.         }*/
    94.         if (other.name == "HealthRegen")
    95.         {
    96.             curHealth += 10;
    97.         }
    98.         if (curHealth > maxHealth)
    99.         {
    100.             curHealth = maxHealth;
    101.         }
    102.         if (curHealth < 0)
    103.         {
    104.             curHealth = 0;
    105.         }
    106.     }
    107.  
    108.  
    109.  
    110.     protected override void Attack()
    111.     {
    112.         if (Input.GetMouseButtonDown (1))
    113.         {
    114.             if(opponent != null && Vector3.Distance(opponent.position, transform.position) < attackRange)
    115.             {
    116.                 opponent.GetComponent<Enemy>().GetHit(damage);
    117.  
    118.                 attacking = true;
    119.  
    120.                 block = true;
    121.  
    122.                 Invoke("UnBlock", attackSpeed);
    123.             }
    124.  
    125.             if(Vector3.Distance(opponent.position, transform.position) < attackRange && opponent != null)
    126.             {
    127.                 attacking = true;
    128.             }
    129.             else
    130.             {
    131.                 attacking = false;
    132.             }
    133.             anim.SetBool ("IsAttacking", attacking);
    134.         }
    135.     }
    136.  
    137.  
    138.  
    139.     void UnBlock()
    140.     {
    141.         block = false;
    142.     }
    143. }
    144.  
     
  2. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    Code (CSharp):
    1. if (Input.GetMouseButtonDown (1))
    2.         {
    3.             if(opponent != null && Vector3.Distance(opponent.position, transform.position) < attackRange)
    4.             {
    5.                 opponent.GetComponent<Enemy>().GetHit(damage);
    6.                 attacking = true;
    7.                 block = true;
    8.                 Invoke("UnBlock", attackSpeed);
    9.             }
    10.             if(Vector3.Distance(opponent.position, transform.position) < attackRange && opponent != null)
    11.             {
    12.                 attacking = true;
    13.             }
    14.             else
    15.             {
    16.                 attacking = false;
    17.             }
    18.             anim.SetBool ("IsAttacking", attacking);
    19.         }
    20. else
    21. {
    22. if(opponent != null && Vector3.Distance( opponent.position, transform.position) > attackRange)
    23.             {
    24.                  anim.SetBool ("IsAttacking", false);
    25.             }
    26.          
    27. }
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    the problem persist, the attack animation just play one time... and not everytime i click right muse button... it gives damage every time i click but animation jkust play in the 1st attack...
     
  4. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    I think the animation is looping, did you tried with loop ?
     
  5. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    i try with booth.. if not looping i need walk between attack to attack animation work again, if i loo, the animation never stop playing hitting only one single time, i need animation to stop after each hit and work again when i attack again..
     
  6. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    change IsAttacking to trigger
     
  7. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    and i need change enything in code then? and how i connect the attack with other states in animator?
     
  8. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    just give it a try you will figure out how to do it

    and yes in your code you need to call anim.SeTrigger("IsAttacking")
     
  9. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    dont work ty anyway if someone else has an idea i will appreciate. ty
     
  10. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    could you post your code please ?
     
  11. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    the code is up in the thread
     
  12. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    anyone plz??? Bump!!
     
  13. holliebuckets

    holliebuckets

    Moderator

    Joined:
    Oct 23, 2014
    Posts:
    496
    I think you have it half right :) You have a GetButtonDown to start the animation, but you need a GetButtonUp to stop the animation :) That is what I would suggest ^_^

    You could keep it a bool or use multiple triggers. (ie. SetTrigger("Attack") and SetTrigger("StopAttack") or something like that). Just a thought :D but I am sure there are multiple ways of making it work ^_^
     
  14. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    So u tell me to use getmousebutton up instead of down? or other way use more bools to start and stop ??? i really dont like mecanims xD or use button down to start and button up to stop?
     
  15. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    im more confused then i dont know what... the idle and move part was simple but now implement the atack in the idle/move omg... cuz the logixc and damage is working just fine... but implmente the animations no coments...


    and i think the problems is that i hv two scripts 1 to clicktomove and other to player stats and attack... and the things are going like crazy thx to inventory system and character window

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Player : HumanoidControl
    6. {
    7.     private static Player instance;
    8.    
    9.     public static Player Instance
    10.     {
    11.         get
    12.         {
    13.             if(instance == null)
    14.             {
    15.                 instance = GameObject.FindObjectOfType<Player>();
    16.             }
    17.             return Player.instance;
    18.         }
    19.     }
    20.    
    21.     public Image healthBar;
    22.    
    23.     public Text healthtext;
    24.    
    25.     public static Transform opponent;
    26.    
    27.     public static Player player;
    28.    
    29.     bool block;
    30.    
    31.     private Animator anim;
    32.    
    33.     private bool attacking;
    34.    
    35.    
    36.     //character base status
    37.     public int baseIntellect;
    38.     public int baseAgility;
    39.     public int baseStrength;
    40.     public int baseStamina;
    41.    
    42.     //character modified status with equipment
    43.     private int intellect;
    44.     private int agility;
    45.     private int strength;
    46.     private int stamina;
    47.    
    48.     public Text statsText;
    49.    
    50.     void Awake()
    51.     {
    52.         player = this;
    53.        
    54.         anim = GetComponent<Animator>();
    55.     }
    56.    
    57.    
    58.     void Start()
    59.     {
    60.         curHealth = maxHealth;
    61.        
    62.         SetStatus (0, 0, 0, 0);
    63.     }
    64.    
    65.    
    66.     void Update()
    67.     {
    68.         healthBar.fillAmount = curHealth / maxHealth;
    69.        
    70.         healthtext.text = "Health: " + curHealth + "/" + maxHealth;
    71.        
    72.         Attack();
    73.     }
    74.    
    75.    
    76.     public void SetStatus(int intellect, int agility, int strength, int stamina)
    77.     {
    78.         this.intellect = intellect + baseIntellect;
    79.         this.agility = agility + baseAgility;
    80.         this.strength = strength + baseStrength;
    81.         this.stamina = stamina + baseStamina;
    82.        
    83.         statsText.text = string.Format ("Intellect: {0}\nAgility: {1}\nStrength: {2}\nStamina: {3}", this.intellect, this.agility, this.strength, this.stamina);
    84.     }
    85.    
    86.    
    87.    
    88.     void OnTriggerStay(Collider other)
    89.     {
    90.         if (other.name == "HealthRegen")
    91.         {
    92.             curHealth += 10;
    93.         }
    94.         if (curHealth > maxHealth)
    95.         {
    96.             curHealth = maxHealth;
    97.         }
    98.         if (curHealth < 0)
    99.         {
    100.             curHealth = 0;
    101.         }
    102.     }
    103.    
    104.    
    105.    
    106.     protected override void Attack()
    107.     {
    108.         if (Input.GetMouseButtonUp (1))
    109.         {
    110.             if(opponent != null && Vector3.Distance(opponent.position, transform.position) < attackRange)
    111.             {
    112.                 opponent.GetComponent<Enemy>().GetHit(damage);
    113.                
    114.                 attacking = true;
    115.  
    116.                 block = true;
    117.                
    118.                 Invoke("UnBlock", attackSpeed);
    119.             }
    120.            
    121.             if(Vector3.Distance(opponent.position, transform.position) < attackRange && opponent != null)
    122.             {
    123.                 attacking = true;
    124.             }
    125.             else
    126.             {
    127.                 attacking = false;
    128.             }
    129.             anim.SetBool ("IsAttacking", attacking);
    130.         }
    131.     }
    132.    
    133.    
    134.    
    135.     void UnBlock()
    136.     {
    137.         block = false;
    138.     }
    139. }
    140.  
    141.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6. //[RequireComponent(typeof(NavMeshAgent))]      
    7. public class ClickToMove : MonoBehaviour
    8. {
    9.     private Vector3 targetPosition;              
    10.  
    11.     const int LEFT_MOUSE_BUTTON = 0;          
    12.  
    13.     NavMeshAgent navAgent;
    14.  
    15.     private Animator anim;
    16.  
    17.     private bool running;
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Awake ()
    22.     {
    23.         anim = GetComponent<Animator>();
    24.  
    25.         navAgent = GetComponent<NavMeshAgent>();
    26.     }
    27.  
    28.  
    29.  
    30.     void Start()
    31.     {
    32.         targetPosition = transform.position;
    33.     }
    34.  
    35.  
    36.  
    37.     // Update is called once per frame
    38.     void Update ()
    39.     {
    40.         if (!EventSystem.current.IsPointerOverGameObject())
    41.         {
    42.             if (Input.GetMouseButton (LEFT_MOUSE_BUTTON))
    43.                 SetTargetPosition ();
    44.  
    45.             MovePlayer ();
    46.         }
    47.     }
    48.  
    49.  
    50.  
    51.     void SetTargetPosition()
    52.     {
    53.         Plane plane = new Plane(Vector3.up, transform.position);
    54.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    55.         float point = 0;
    56.  
    57.         if (plane.Raycast (ray, out point))
    58.             targetPosition = ray.GetPoint (point);
    59.     }
    60.  
    61.  
    62.  
    63.     private void MovePlayer()
    64.     {
    65.         if (GameObject.Find ("MouseOverUI").GetComponent<MouseOverUI>().IsmouseOverGUI)
    66.         {
    67.  
    68.         }
    69.         else
    70.         {
    71.             running = true;
    72.             navAgent.SetDestination(targetPosition);
    73.             navAgent.Resume();
    74.         }
    75.  
    76.         if (navAgent.remainingDistance <= navAgent.stoppingDistance)
    77.         {
    78.             running = false;
    79.         }
    80.         else
    81.         {
    82.             running = true;
    83.         }
    84.         anim.SetBool ("IsRunning", running);
    85.     }
    86. }
     
    Last edited: Sep 18, 2015
  16. holliebuckets

    holliebuckets

    Moderator

    Joined:
    Oct 23, 2014
    Posts:
    496
    I am so sorry that was very confusing the way I wrote it.

    I think you need a second method of GetButtonUp so you can tell the Animator when to stop the animation :) I have a personal example script at home but we just moved so its packed :/ sorry I couldn't be of more help!
     
  17. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Appreciate anyway i will try some more things or w8 some kind of help that could appear ;) Thx m8

    and all tutorials for mecanim are for 3rd person view no one for isometric click to move / attack...
     
    Last edited: Sep 18, 2015
    holliebuckets likes this.
  18. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    After some testes i see that when i first hit the attack animation play but after that never stop... the bar above the attack clip in animator is always full so it want treturn to idle... and so never play again... can anyonbe help me plz?
     
  19. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    The probleme you have is "you don't say to mecanim to finish the attack state", this is why you see the bar full

    What you can do :

    you can use IsAttacking as bool, in that case you need to call anim.SetBool("IsAttacking",false)

    from your code this call is inside if(Input.GetMouseButtonDown(1))

    so it'll be execute till the next mouse down, you have to move it from there and check if the normalizetime >= 1, to set it to false

    the simple way to do it is with trigger,, when you call anim.SetTrigger("IsAttacking"), you don't worry about finish or reseting your state, it'll be automatic if the transition has exit time (this is by default when you create a transition)

    Code (CSharp):
    1.  protected override void Attack()
    2.     {
    3.         if (Input.GetMouseButtonDowm (1))
    4.         {
    5.             if(opponent != null && Vector3.Distance(opponent.position, transform.position) < attackRange)
    6.             {
    7.                 opponent.GetComponent<Enemy>().GetHit(damage);
    8.              
    9.                 attacking = true;
    10.                 block = true;
    11.              
    12.                 Invoke("UnBlock", attackSpeed);
    13.             }
    14.          
    15.             if(Vector3.Distance(opponent.position, transform.position) < attackRange && opponent != null)
    16.             {
    17.                 attacking = true;
    18.          
    19.             anim.SetTrigger ("IsAttacking");
    20.         }
    21.     }
    right now in the transition you have IsTrigger == true to play the attack (I am guessing...) if you change it to trigger you will not see any the true/false param, to get back to idle you don't need any transition condition just the "has exit time" is fine


    Hope that help
     
  20. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Finally... Ty m8... Just one other question its possible to player give damage to opponent at certain time of animation?, for example 0.037% of animation time?
     
  21. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    animator.GetCurrentAnimatorStateInfo(0).normalizetime gives you the current time normalized [0,1]

    0 is the begin
    1 is the end of the animation

    animator.GetCurrentAnimatorStateInfo(0).normalizetime == 0.037 will not work unless

    you make something animator.GetCurrentAnimatorStateInfo(0).normalizetime > val1 && < val2

    the other way is by adding an animation event
     
  22. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    so if i understant i need put this after i activate the attack animation?

    animator.GetCurrentAnimatorStateInfo(0).normalizetime > 0.36 && < 0.38 ???
     
  23. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    Add this to your update function

    Code (CSharp):
    1. animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);              
    2.  
    3. if (animatorStateInfo.IsName("Attack") && animatorStateInfo.normalizedTime > 0.36f && animatorStateInfo.normalizedTime < 0.38f )
    4. {
    5.  
    6. }
     
  24. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    if i do this i get 2 errors,


    }[/code][/QUOTE]
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Player : HumanoidControl
    6. {
    7.     private static Player instance;
    8.  
    9.     public static Player Instance
    10.     {
    11.         get
    12.         {
    13.             if(instance == null)
    14.             {
    15.                 instance = GameObject.FindObjectOfType<Player>();
    16.             }
    17.             return Player.instance;
    18.         }
    19.     }
    20.  
    21.     public Image healthBar;
    22.  
    23.     public Text healthtext;
    24.  
    25.     public static Transform opponent;
    26.  
    27.     public static Player player;
    28.  
    29.     bool block;
    30.  
    31.     private Animator anim;
    32.  
    33.     private bool attacking;
    34.  
    35.  
    36.     //character base status
    37.     public int baseIntellect;
    38.     public int baseAgility;
    39.     public int baseStrength;
    40.     public int baseStamina;
    41.  
    42.     //character modified status with equipment
    43.     private int intellect;
    44.     private int agility;
    45.     private int strength;
    46.     private int stamina;
    47.  
    48.     public Text statsText;
    49.  
    50.  
    51.  
    52.     void Awake()
    53.     {
    54.         player = this;
    55.      
    56.         anim = GetComponent<Animator>();
    57.     }
    58.  
    59.  
    60.  
    61.     void Start()
    62.     {
    63.         curHealth = maxHealth;
    64.              
    65.         SetStatus (0, 0, 0, 0);
    66.     }
    67.  
    68.  
    69.  
    70.     void Update()
    71.     {
    72.         healthBar.fillAmount = curHealth / maxHealth;
    73.      
    74.         healthtext.text = "Health: " + curHealth + "/" + maxHealth;
    75.      
    76.         Attack();
    77.  
    78.         AnimatorStateInfo = anim.GetCurrentAnimatorStateInfo (0);
    79.  
    80.         if(AnimatorStateInfo.IsName("Attack") && AnimatorStateInfo.normalizedTime > 0.36 && AnimatorStateInfo.normalizedTime < 0.37)
    81.         {
    82.  
    83.         }
    84.     }
    85.  
    86.  
    87.  
    88.     public void SetStatus(int intellect, int agility, int strength, int stamina)
    89.     {
    90.         this.intellect = intellect + baseIntellect;
    91.         this.agility = agility + baseAgility;
    92.         this.strength = strength + baseStrength;
    93.         this.stamina = stamina + baseStamina;
    94.      
    95.         statsText.text = string.Format ("Intellect: {0}\nAgility: {1}\nStrength: {2}\nStamina: {3}", this.intellect, this.agility, this.strength, this.stamina);
    96.     }
    97.  
    98.  
    99.  
    100.     void OnTriggerStay(Collider other)
    101.     {
    102.         if (other.name == "HealthRegen")
    103.         {
    104.             curHealth += 10;
    105.         }
    106.         if (curHealth > maxHealth)
    107.         {
    108.             curHealth = maxHealth;
    109.         }
    110.         if (curHealth < 0)
    111.         {
    112.             curHealth = 0;
    113.         }
    114.     }
    115.  
    116.  
    117.  
    118.     protected override void Attack()
    119.     {
    120.         if (Input.GetMouseButtonUp (0))
    121.         {
    122.             if(opponent != null && Vector3.Distance(opponent.position, transform.position) < attackRange)
    123.             {
    124.                 if(!block)
    125.                 {
    126.                     transform.LookAt(opponent);
    127.  
    128.                     opponent.GetComponent<Enemy>().GetHit(damage);
    129.                  
    130.                     attacking = true;
    131.  
    132.                     anim.SetTrigger("IsAttacking");
    133.  
    134.                     block = true;
    135.                  
    136.                     Invoke("UnBlock", attackSpeed);
    137.                 }
    138.             }
    139.         }
    140.     }
    141.  
    142.  
    143.  
    144.     void UnBlock()
    145.     {
    146.         block = false;
    147.     }
    148. }
    149.  
    150.  

    Assets/Scripts/PlayerScripts/Player.cs(78,17): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer


    Assets/Scripts/PlayerScripts/Player.cs(80,38): error CS0120: An object reference is required to access non-static member `UnityEngine.AnimatorStateInfo.IsName(string)'
     
  25. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    AnimatorStateInfo animStateInfo = anim.GetCurrentAnimatorStateInfo(0);
     
  26. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    wt is supposed to put between {}

    1. AnimatorStateInfo animStateInfo = anim.GetCurrentAnimatorStateInfo(0);

    2. if (animatorStateInfo.IsName("Attack") && animatorStateInfo.normalizedTime > 0.36f && animatorStateInfo.normalizedTime < 0.38f )
    3. {
    4. //wts supposed to put here???
    5. }


    srry for taking ur time
     
  27. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    Just one other question its possible to player give damage to opponent at certain time of animation?
     
  28. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Yes. Right now it gives damage in the exact second i click left mouse button over hit... instead of 0.37% of animation time...

    so first enemy get hit and then animation plays xD
     
  29. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    1. if (animatorStateInfo.IsName("Attack") && animatorStateInfo.normalizedTime > 0.36f && animatorStateInfo.normalizedTime < 0.38f )
    this line means that animation attack is playing, and its reaching some point between 0.36 and 0.38, so you can apply the damage if it's true
     
  30. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    so if i understand i use this in update, and then i can do this?

    Code (CSharp):
    1. AnimatorStateInfo animStateInfo = anim.GetCurrentAnimatorStateInfo(0);
    2.  
    3.         if (animStateInfo.IsName("Attack") && animStateInfo.normalizedTime == 0.36f && animStateInfo.normalizedTime < 0.38f )
    4.         {
    5.             opponent.GetComponent<Enemy>().GetHit(damage);
    6.         }
    or i need call Attack(); in between {} ??
     
  31. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    i think like this is fine, just change animStateInfo.normalizedTime== 0.36f to > 0.36f
     
  32. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    ... The max i can do is to damage at mouse click... just probably create and event in attack clip but i dont know how to implement it...
     
  33. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Setting a range like animStateInfo.normalizedTime== 0.36f && animStateInfo.normalizedTime< 0.38f is not a good idea, if you get an update call at 0.35f and then the next one at 0.39f you won't send the damage message.

    you should use a bool and change your condition to something like this
    Code (CSharp):
    1.  
    2. if (animStateInfo.IsName("Attack") && !sendDamage && animStateInfo.normalizedTime > 0.36f )
    3. {
    4.     sendDamage = false;  
    5.     opponent.GetComponent<Enemy>().GetHit(damage);
    6. }
    and don't forget to set back your bool sendDamage to true when you call your Attack function
     
  34. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    this is so annoying now i play attack animation but i give no damage, and after i atack if i run and stop it play attack animation alone again... jesus is that so hard...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Player : HumanoidControl
    6. {
    7.     private static Player instance;
    8.  
    9.     public static Player Instance
    10.     {
    11.         get
    12.         {
    13.             if(instance == null)
    14.             {
    15.                 instance = GameObject.FindObjectOfType<Player>();
    16.             }
    17.             return Player.instance;
    18.         }
    19.     }
    20.  
    21.     public Image healthBar;
    22.  
    23.     public Text healthtext;
    24.  
    25.     public static Transform opponent;
    26.  
    27.     public static Player player;
    28.  
    29.     bool block;
    30.  
    31.     private Animator anim;
    32.  
    33.     private bool attacking;
    34.  
    35.     private bool running;
    36.  
    37.     private bool sendDamage;
    38.  
    39.  
    40.     //character base status
    41.     public int baseIntellect;
    42.     public int baseAgility;
    43.     public int baseStrength;
    44.     public int baseStamina;
    45.  
    46.     //character modified status with equipment
    47.     private int intellect;
    48.     private int agility;
    49.     private int strength;
    50.     private int stamina;
    51.  
    52.     public Text statsText;
    53.  
    54.  
    55.  
    56.     void Awake()
    57.     {
    58.         player = this;
    59.    
    60.         anim = GetComponent<Animator>();
    61.     }
    62.  
    63.  
    64.  
    65.     void Start()
    66.     {
    67.         curHealth = maxHealth;
    68.            
    69.         SetStatus (0, 0, 0, 0);
    70.     }
    71.  
    72.  
    73.  
    74.     void Update()
    75.     {
    76.         healthBar.fillAmount = curHealth / maxHealth;
    77.    
    78.         healthtext.text = "Health: " + curHealth + "/" + maxHealth;
    79.    
    80.         Attack();
    81.  
    82.         AnimatorStateInfo animStateInfo = anim.GetCurrentAnimatorStateInfo(0);
    83.  
    84.         if (animStateInfo.IsName("Attack") && !sendDamage && animStateInfo.normalizedTime > 0.36f)
    85.         {
    86.             sendDamage = false;
    87.  
    88.             opponent.GetComponent<Enemy>().GetHit(damage);
    89.         }
    90.     }
    91.  
    92.  
    93.  
    94.     public void SetStatus(int intellect, int agility, int strength, int stamina)
    95.     {
    96.         this.intellect = intellect + baseIntellect;
    97.         this.agility = agility + baseAgility;
    98.         this.strength = strength + baseStrength;
    99.         this.stamina = stamina + baseStamina;
    100.    
    101.         statsText.text = string.Format ("Intellect: {0}\nAgility: {1}\nStrength: {2}\nStamina: {3}", this.intellect, this.agility, this.strength, this.stamina);
    102.     }
    103.  
    104.  
    105.  
    106.     void OnTriggerStay(Collider other)
    107.     {
    108.         if (other.name == "HealthRegen")
    109.         {
    110.             curHealth += 10;
    111.         }
    112.         if (curHealth > maxHealth)
    113.         {
    114.             curHealth = maxHealth;
    115.         }
    116.     }
    117.  
    118.  
    119.  
    120.     protected override void Attack()
    121.     {
    122.         if (Input.GetMouseButtonUp (0))
    123.         {
    124.             if(opponent != null && Vector3.Distance(opponent.position, transform.position) < attackRange)
    125.             {
    126.                 if(!block)
    127.                 {
    128.                     sendDamage = true;
    129.  
    130.                     transform.LookAt(opponent);
    131.  
    132.                     opponent.GetComponent<Enemy>().GetHit(damage);
    133.                    
    134.                     attacking = true;
    135.  
    136.                     anim.SetTrigger("IsAttacking");
    137.  
    138.                     block = true;
    139.  
    140.                     Invoke("UnBlock", attackSpeed);
    141.                 }
    142.  
    143.                 if(Vector3.Distance(opponent.position, transform.position) < attackRange && opponent != null)
    144.                 {
    145.                     attacking = true;
    146.                 }
    147.                 else
    148.                 {
    149.                     attacking = false;
    150.                 }
    151.                 anim.SetBool ("IsAttacking", attacking);
    152.             }
    153.         }
    154.     }
    155.  
    156.  
    157.  
    158.     void UnBlock()
    159.     {
    160.         block = false;
    161.     }
    162. }

    i had 4 variables in animator:
    - bool IsRunning
    - trigger Death
    - trigger IsAttacking



    could my click to move script interfere with this problem???

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6. //[RequireComponent(typeof(NavMeshAgent))]      
    7. public class ClickToMove : MonoBehaviour
    8. {
    9.     private Vector3 targetPosition;              
    10.  
    11.     const int LEFT_MOUSE_BUTTON = 0;          
    12.  
    13.     NavMeshAgent navAgent;
    14.  
    15.     private Animator anim;
    16.  
    17.     private bool running;
    18.  
    19.     private bool attacking;
    20.  
    21.  
    22.     // Use this for initialization
    23.     void Awake ()
    24.     {
    25.         anim = GetComponent<Animator>();
    26.  
    27.         navAgent = GetComponent<NavMeshAgent>();
    28.     }
    29.  
    30.  
    31.  
    32.     void Start()
    33.     {
    34.         targetPosition = transform.position;
    35.     }
    36.  
    37.  
    38.  
    39.     // Update is called once per frame
    40.     void Update ()
    41.     {
    42.         if (!EventSystem.current.IsPointerOverGameObject())
    43.         {
    44.             if (Input.GetMouseButton (LEFT_MOUSE_BUTTON))
    45.                 SetTargetPosition ();
    46.  
    47.             MovePlayer ();
    48.         }
    49.     }
    50.  
    51.  
    52.  
    53.     void SetTargetPosition()
    54.     {
    55.         Plane plane = new Plane(Vector3.up, transform.position);
    56.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    57.         float point = 0;
    58.  
    59.         if (plane.Raycast (ray, out point))
    60.             targetPosition = ray.GetPoint (point);
    61.     }
    62.  
    63.  
    64.  
    65.     private void MovePlayer()
    66.     {
    67.         if (GameObject.Find ("MouseOverUI").GetComponent<MouseOverUI>().IsmouseOverGUI)
    68.         {
    69.  
    70.         }
    71.         else
    72.         {
    73.             running = true;
    74.             attacking = false;
    75.             navAgent.SetDestination(targetPosition);
    76.             navAgent.Resume();
    77.         }
    78.  
    79.         if (navAgent.remainingDistance <= navAgent.stoppingDistance)
    80.         {
    81.             running = false;
    82.         }
    83.         else
    84.         {
    85.             running = true;
    86.             attacking = false;
    87.         }
    88.         anim.SetBool ("IsRunning", running);
    89.     }
    90. }
     
    Last edited: Sep 18, 2015
  35. MadeThisName

    MadeThisName

    Joined:
    Mar 14, 2015
    Posts:
    115
    Thanks Simo, I asked a similar question and this was the answer i was looking for.
     
  36. MadeThisName

    MadeThisName

    Joined:
    Mar 14, 2015
    Posts:
    115
    Create a float called Shot in your Animator/Parameters. Create another float on your Animation Transition state curve, also call it Shot. Give the Shot curve a spike when the attack is fully preformed, Call the float via script like this.

    Code (CSharp):
    1.  
    2.             float shot = anim.GetFloat("Shot");
    3.             if (!sendDamage && shot > 0.36f)
    4.             {
    5.              //Do somthing
    6.             }
    hope it helps GL
     
    Last edited: Sep 19, 2015
  37. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    I will try later im not at home. Só if i understand i need create another para meter and in the trânsito i ser isattaking and shot??? And then call it com script is that righr??? Ty
     
  38. MadeThisName

    MadeThisName

    Joined:
    Mar 14, 2015
    Posts:
    115
    Sorry but my instructions couldn't be more clear. Try Google translate encase you missed any wording. :)
     
  39. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    ok i test but if i create a curve in my animation at 0.36 called Shot and a float Shot in animator paramenters and do that code my player dont even get damage to the enemy...

    so any other idea?? ty
     
  40. MadeThisName

    MadeThisName

    Joined:
    Mar 14, 2015
    Posts:
    115
    Try adding Debug to all the appropriate code to see whats not getting called
     
  41. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Ok i get it to work but using all methods u tell me in update function but the problem is that player over damage over second and is like it kills the enemy multiple time cuz my exp rises like to 1 to 36... i try change the method to the attack method but then i get no damage in enemy... wt am i doing worng??

    with this code it hits in current time... i create the float Shoot in animator and animation as u tell me.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Player : HumanoidControl
    6. {
    7.     private static Player instance;
    8.  
    9.     public static Player Instance
    10.     {
    11.         get
    12.         {
    13.             if(instance == null)
    14.             {
    15.                 instance = GameObject.FindObjectOfType<Player>();
    16.             }
    17.             return Player.instance;
    18.         }
    19.     }
    20.  
    21.     public Image healthBar;
    22.  
    23.     public Text healthtext;
    24.  
    25.     public static Transform opponent;
    26.  
    27.     public static Player player;
    28.  
    29.     bool block;
    30.  
    31.     private Animator anim;
    32.  
    33.     private bool attacking;
    34.  
    35.     private bool running;
    36.  
    37.     private bool sendDamage;
    38.  
    39.  
    40.     //character base status
    41.     public int baseIntellect;
    42.     public int baseAgility;
    43.     public int baseStrength;
    44.     public int baseStamina;
    45.  
    46.     //character modified status with equipment
    47.     private int intellect;
    48.     private int agility;
    49.     private int strength;
    50.     private int stamina;
    51.  
    52.     public Text statsText;
    53.  
    54.  
    55.  
    56.     void Awake()
    57.     {
    58.         player = this;
    59.      
    60.         anim = GetComponent<Animator>();
    61.     }
    62.  
    63.  
    64.  
    65.     void Start()
    66.     {
    67.         curHealth = maxHealth;
    68.              
    69.         SetStatus (0, 0, 0, 0);
    70.     }
    71.  
    72.  
    73.  
    74.     void Update()
    75.     {
    76.         healthBar.fillAmount = curHealth / maxHealth;
    77.      
    78.         healthtext.text = "Health: " + curHealth + "/" + maxHealth;
    79.      
    80.         Attack();
    81.  
    82.         AnimatorStateInfo animStateInfo = anim.GetCurrentAnimatorStateInfo(0);
    83.  
    84.         if (animStateInfo.IsName("Attack") && !sendDamage && animStateInfo.normalizedTime > 0.36f)
    85.         {
    86.  
    87.             float Shot = anim.GetFloat("Shoot");
    88.  
    89.             if(!sendDamage && Shot > 0.36f)
    90.                 opponent.GetComponent<Enemy>().GetHit(damage);
    91.         }
    92.     }
    93.  
    94.  
    95.  
    96.     public void SetStatus(int intellect, int agility, int strength, int stamina)
    97.     {
    98.         this.intellect = intellect + baseIntellect;
    99.         this.agility = agility + baseAgility;
    100.         this.strength = strength + baseStrength;
    101.         this.stamina = stamina + baseStamina;
    102.  
    103.         statsText.text = string.Format ("Intellect: {0}\nAgility: {1}\nStrength: {2}\nStamina: {3}", this.intellect, this.agility, this.strength, this.stamina);
    104.     }
    105.  
    106.  
    107.  
    108.     void OnTriggerStay(Collider other)
    109.     {
    110.         if (other.name == "HealthRegen")
    111.         {
    112.             curHealth += 10;
    113.         }
    114.         if (curHealth > maxHealth)
    115.         {
    116.             curHealth = maxHealth;
    117.         }
    118.     }
    119.  
    120.  
    121.  
    122.     protected override void Attack()
    123.     {
    124.         if (Input.GetMouseButtonUp (1))
    125.         {
    126.             if(opponent != null && Vector3.Distance(opponent.position, transform.position) < attackRange)
    127.             {
    128.                 if(!block)
    129.                 {
    130.                     float shoot = anim.GetFloat("Shoot");
    131.  
    132.                     if(shoot > 0.36f)
    133.                     {
    134.                         sendDamage = true;
    135.  
    136.                         transform.LookAt(opponent);
    137.  
    138.                         opponent.GetComponent<Enemy>().GetHit(damage);
    139.                              
    140.                         attacking = true;
    141.  
    142.                         anim.SetTrigger("IsAttacking");
    143.  
    144.                         block = true;
    145.  
    146.                         Invoke("UnBlock", attackSpeed);
    147.                     }
    148.                 }
    149.  
    150.                 if(Vector3.Distance(opponent.position, transform.position) < attackRange && opponent != null)
    151.                 {
    152.                     attacking = true;
    153.                 }
    154.                 else
    155.                 {
    156.                     attacking = false;
    157.                 }
    158.                 anim.SetBool ("IsAttacking", attacking);
    159.             }
    160.         }
    161.     }
    162.  
    163.  
    164.  
    165.     void UnBlock()
    166.     {
    167.         block = false;
    168.     }
    169. }
    170.  
    171.  

    if i use the code this way the damge apply fine but out of time,, so i get 2 in between wt i want to accomplish -.-

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Player : HumanoidControl
    6. {
    7.     private static Player instance;
    8.    
    9.     public static Player Instance
    10.     {
    11.         get
    12.         {
    13.             if(instance == null)
    14.             {
    15.                 instance = GameObject.FindObjectOfType<Player>();
    16.             }
    17.             return Player.instance;
    18.         }
    19.     }
    20.    
    21.     public Image healthBar;
    22.    
    23.     public Text healthtext;
    24.    
    25.     public static Transform opponent;
    26.    
    27.     public static Player player;
    28.    
    29.     bool block;
    30.    
    31.     private Animator anim;
    32.    
    33.     private bool attacking;
    34.  
    35.     private bool running;
    36.  
    37.     private bool sendDamage;
    38.  
    39.    
    40.     //character base status
    41.     public int baseIntellect;
    42.     public int baseAgility;
    43.     public int baseStrength;
    44.     public int baseStamina;
    45.    
    46.     //character modified status with equipment
    47.     private int intellect;
    48.     private int agility;
    49.     private int strength;
    50.     private int stamina;
    51.    
    52.     public Text statsText;
    53.  
    54.  
    55.  
    56.     void Awake()
    57.     {
    58.         player = this;
    59.        
    60.         anim = GetComponent<Animator>();
    61.     }
    62.    
    63.  
    64.  
    65.     void Start()
    66.     {
    67.         curHealth = maxHealth;
    68.                
    69.         SetStatus (0, 0, 0, 0);
    70.     }
    71.  
    72.  
    73.    
    74.     void Update()
    75.     {
    76.         healthBar.fillAmount = curHealth / maxHealth;
    77.        
    78.         healthtext.text = "Health: " + curHealth + "/" + maxHealth;
    79.        
    80.         Attack();
    81.  
    82.         AnimatorStateInfo animStateInfo = anim.GetCurrentAnimatorStateInfo(0);
    83.  
    84.         if (animStateInfo.IsName("Attack") && !sendDamage && animStateInfo.normalizedTime > 0.36f)
    85.         {
    86.             //sendDamage = false;
    87.  
    88.             //opponent.GetComponent<Enemy>().GetHit(damage);
    89.  
    90.             float Shot = anim.GetFloat("Shoot");
    91.  
    92.             if(!sendDamage && Shot > 0.36f)
    93.                 opponent.GetComponent<Enemy>().GetHit(damage);
    94.         }
    95.     }
    96.  
    97.  
    98.    
    99.     public void SetStatus(int intellect, int agility, int strength, int stamina)
    100.     {
    101.         this.intellect = intellect + baseIntellect;
    102.         this.agility = agility + baseAgility;
    103.         this.strength = strength + baseStrength;
    104.         this.stamina = stamina + baseStamina;
    105.  
    106.         statsText.text = string.Format ("Intellect: {0}\nAgility: {1}\nStrength: {2}\nStamina: {3}", this.intellect, this.agility, this.strength, this.stamina);
    107.     }
    108.    
    109.    
    110.    
    111.     void OnTriggerStay(Collider other)
    112.     {
    113.         if (other.name == "HealthRegen")
    114.         {
    115.             curHealth += 10;
    116.         }
    117.         if (curHealth > maxHealth)
    118.         {
    119.             curHealth = maxHealth;
    120.         }
    121.     }
    122.    
    123.    
    124.    
    125.     protected override void Attack()
    126.     {
    127.         if (Input.GetMouseButtonUp (1))
    128.         {
    129.             if(opponent != null && Vector3.Distance(opponent.position, transform.position) < attackRange)
    130.             {
    131.                 if(!block)
    132.                 {
    133.                     //float shoot = anim.GetFloat("Shoot");
    134.  
    135.                     //if(shoot > 0.36f)
    136.                     //{
    137.                         sendDamage = true;
    138.  
    139.                         transform.LookAt(opponent);
    140.  
    141.                         opponent.GetComponent<Enemy>().GetHit(damage);
    142.                                
    143.                         attacking = true;
    144.  
    145.                         anim.SetTrigger("IsAttacking");
    146.  
    147.                         block = true;
    148.  
    149.                         Invoke("UnBlock", attackSpeed);
    150.                     //}
    151.                 }
    152.  
    153.                 if(Vector3.Distance(opponent.position, transform.position) < attackRange && opponent != null)
    154.                 {
    155.                     attacking = true;
    156.                 }
    157.                 else
    158.                 {
    159.                     attacking = false;
    160.                 }
    161.                 anim.SetBool ("IsAttacking", attacking);
    162.             }
    163.         }
    164.     }
    165.    
    166.    
    167.    
    168.     void UnBlock()
    169.     {
    170.         block = false;
    171.     }
    172. }
    173.  
    174.  
     
    Last edited: Sep 27, 2015
  42. MadeThisName

    MadeThisName

    Joined:
    Mar 14, 2015
    Posts:
    115
    Try this but you really should consider taking a close look at what you are doing since your code lacks logic and understanding.

    Code (CSharp):
    1. void Update()
    2. {
    3.    if (Input.GetMouseButtonDown (1) && !attacking)
    4.    {
    5.         float shot = anim.GetFloat("Shoot");
    6.         if(shot > 0.36f)
    7.         {
    8.              attacking = true;
    9.              Attack();
    10.         }
    11.    }
    12. }
    13.    
    Code (CSharp):
    1. protected override void Attack()
    2.     {
    3.         if(opponent != null && Vector3.Distance(opponent.position, transform.position) < attackRange)
    4.         {
    5.             if(!block)
    6.             {
    7. //If you use this make sure your animation has exit time enabled since the second condition is attacking and that gets set to false right after!
    8.                // anim.SetBool ("IsAttacking", attacking);
    9.  
    10. //A trigger plays the animation  once then defaults to false you can use this instead of the one above but not both.
    11.                 anim.SetTrigger("IsAttacking");
    12.  
    13.                 attacking = false;
    14.             }
    15.         }
    16.     }
     
    Last edited: Sep 27, 2015