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

Needing help with returning to Walk animation plz.

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

  1. Ceegore

    Ceegore

    Joined:
    Sep 2, 2011
    Posts:
    14
    Hi guys,
    Sorry I've had to come to the site with this, however I'm at a complete lose. I've been working on this(hopefully simple) prb for two days now. I'm sure it's something I just am not seeing or understanding. Here is my situation.

    I have a monster walking around chasing me.
    When he gets within a "distance" he begins to attack me. - Here plays attack animation.
    When the player gets outside the "distance" range (currently set at 2.5f), the animation SHOULD return back to walk, while continuing to pursue player.

    Everything works as it should...except the returning to walk animation when play capsule is outside the range, it just continues to swing at me attacking no matter where the player caps is or how far away it it.

    Can someone please tell me the proper way to go about doing this?
    Thanks so much for any and all help.

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

    Ceegore

    Joined:
    Sep 2, 2011
    Posts:
    14
    Note: After adding in the Debug.Logs on lines 37 42. I've noticed in the console that the scripts is constantly reading "I'm attacking you". Obviously, I've not set something correctly to tell it that the player is out of range. I am currently working with this trying to straighten it out, and I will post here if I do. However any advice and help would be most appreciated. This problem is putting a hold on my entire project. Thanks
     
  3. 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