Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Prb with Animation

Discussion in 'Scripting' started by Ceegore, Jan 6, 2013.

  1. Ceegore

    Ceegore

    Joined:
    Sep 2, 2011
    Posts:
    14
    Hi guys,
    I'm having some trouble with my attack animation script and I was hoping someone could take a look at it and help. I'm simply trying to get a monster to attack me while within a certain range and play his attack animation...(which he does correctly), however I can't get him to return to his default walk animation when not attacking (player out of range). Any help would be greatly appreciated. Please go easy on me, as I'm still quit new to coding. Thanks all.

    Code (csharp):
    1.  
    2. /// EnemyAttack.cs
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class EnemyAttack : MonoBehaviour {
    7.     public GameObject target;
    8.     public float attackTimer;
    9.     public float coolDown;
    10.     public bool isAttacking;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         attackTimer = 0;
    15.         coolDown = 2.0f;
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.         if(attackTimer > 0)
    21.             attackTimer -= Time.deltaTime;
    22.        
    23.         if(attackTimer < 0)
    24.             attackTimer = 0;
    25.        
    26.         if(attackTimer == 0) {
    27.             Attack();
    28.             attackTimer = coolDown;
    29.            
    30.         if(isAttacking)
    31.             animation.CrossFade("2LegsClawsAttackR");
    32.             else
    33.                 if(!isAttacking)
    34.                     animation.CrossFade("2LegsWalk");
    35.         }
    36.     }
    37.    
    38.     private void Attack() {
    39.         float distance = Vector3.Distance(target.transform.position, transform.position);
    40.        
    41.         Vector3 dir = (target.transform.position - transform.position).normalized;
    42.        
    43.         float direction = Vector3.Dot(dir, transform.forward);
    44.        
    45.         if(distance < 2.5f) {
    46.             if(direction > 0) {
    47.                 PlayerHealth eh = (PlayerHealth)target.GetComponent("PlayerHealth");
    48.                 eh.AddjustCurrentHealth(-10);
    49.                 isAttacking = true;
    50.                 }
    51.        
    52.         }
    53.     }
    54. }
    55.  
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You never make isAttacking to be false:
    //attack
    if(distance < 2.5f){
    blah
    }
    else isAttacking = false;
     
  3. Ceegore

    Ceegore

    Joined:
    Sep 2, 2011
    Posts:
    14
    Okay, I'll try that. Thank you.
    Was I wrong in thinking that the "!" would assign it to false by default?

    Line 32:

    else

    if(!isAttacking)

    animation.CrossFade("2LegsWalk");
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    ! can assign, if you use it in this manner:

    isAttacking = !isAttacking;

    or

    isAttacking = !true; (this of course might as well be false)
     
  5. Ceegore

    Ceegore

    Joined:
    Sep 2, 2011
    Posts:
    14
    Thank you for the help guys, ive tried the suggestions and several other things that i can think of, however i must still be missing something obvious. Ive worked on this prb for the last 4hrs with no luck, and needed to walk away before i go crazy. I simply cant figure why when the player runs out of range that the animation will not return to the one it was playing before the Attack() method was called. If anyone has any other ideas I'd sure love to hear em, and again I thank you.
     
    Last edited: Jan 8, 2013
  6. Ceegore

    Ceegore

    Joined:
    Sep 2, 2011
    Posts:
    14
    Just wanted to give an update guys, that I've finally fixed the prb. Thanks for the help.
     
unityunity