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

Animation problem

Discussion in 'Scripting' started by Paykoman, Nov 5, 2014.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys i hv a enemy ai script. everything works fine but now when i put the animation my enemy chase me and when he start hit i take the damage but it dont play attack animation. it just keep running near my wild do the damage. I know thats probably a problem of prioratize the atack animation over the run animation but i try lots of changes and im not getting ther can anyone help plz?

    This is the script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class EnemyAI : MonoBehaviour
    4. {
    5.     float distance;
    6.     float lookAtDistance = 25.0f;
    7.     float chaseRange = 15.0f;
    8.     float damping = 6.0f;
    9.     float range = 1.25f;
    10.     float attackDelay = 1.0f;
    11.     float nextAttackTime = 0.0f;
    12.     public int damage;
    13.     public int speed;
    14.     bool isReturning;
    15.     public AnimationClip idle;
    16.     public AnimationClip run;
    17.     public AnimationClip attack;
    18.     public Transform target;
    19.     private Vector3 initialPosition;
    20.     public CharacterController controller;
    21.     void Start()
    22.     {
    23.         initialPosition = transform.position;
    24.         isReturning = false;
    25.     }
    26.     void Update()
    27.     {
    28.         if (!isReturning)
    29.         {
    30.             distance = Vector3.Distance(target.position, transform.position);
    31.        
    32.             if (distance < lookAtDistance)
    33.             {
    34.                 animation.CrossFade (idle.name);
    35.                 LookAt (target.position);
    36.             }
    37.        
    38.             if(distance > lookAtDistance && distance > chaseRange) //LookDistance is always bigger than the attackRange so i commented it out. //if (distance > lookAtDistance) && distance>attackRange)
    39.             {
    40.                 isReturning = true;
    41.                 animation.CrossFade(run.name);
    42.             }
    43.        
    44.             if (distance < chaseRange)
    45.             {
    46.                 transform.LookAt(target.position);
    47.                 controller.SimpleMove(transform.forward*speed);
    48.                 animation.CrossFade(run.name);
    49.            
    50.             }
    51.        
    52.             if(distance <= range)
    53.             {
    54.                 if(Time.time > nextAttackTime)
    55.                 {
    56.                     nextAttackTime = Time.time + attackDelay;
    57.                     Attack ();
    58.                 }
    59.             }
    60.             else
    61.             {
    62.                 nextAttackTime = Time.time + attackDelay;
    63.             }
    64.         }
    65.         else
    66.         {
    67.             LookAt (initialPosition);
    68.             MovePlayerForward();
    69.        
    70.             distance = Vector3.Distance(initialPosition, transform.position);
    71.             if (distance <20)
    72.             {
    73.                 isReturning = false;
    74.             }
    75.         }
    76.     }
    77.     void LookAt(Vector3 targetPosition)
    78.     {
    79.         Quaternion rotation = Quaternion.LookRotation(targetPosition - transform.position);
    80.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    81.     }
    82.     void MovePlayerForward()
    83.     {
    84.         transform.Translate(Vector3.forward * speed * Time.deltaTime);
    85.     }
    86.     private void Attack()
    87.     {
    88.         float distance = Vector3.Distance (target.transform.position, transform.position);
    89.    
    90.         if (distance < 1.5f)
    91.         {
    92.             animation.Play(attack.name);
    93.             PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth");
    94.             ph.AdjustCurrentHealth(damage);
    95.         }
    96.    
    97.     }
    98. }
     
  2. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Its Fixed Ty