Search Unity

Animating multiple of the same prefab

Discussion in 'Animation' started by atenselah, Aug 22, 2019.

  1. atenselah

    atenselah

    Joined:
    Aug 9, 2018
    Posts:
    1
    Hello forum,
    I have been struggling to find the solution to my problem, perhaps from lack of knowing where to start.

    i have this issue as i have created a spawn object that will spawn 4 skeletons behind a wall, then the skeleton will travel towards me, initially idle animation, then walking animation into attack animation.

    I can not get all 4 to properly animate, while 1 skeleton is working perfectly the other 3 skeletons remain in the idle animation.

    i will inclue my enemy chase/animation script
    and my spawn enemy script
    i have also included a video.

    any help appreciated as i am still learning i hope you can keep it fairly friendly lol.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Chase : MonoBehaviour
    7. {
    8.     public Transform player;
    9.    
    10.     static Animator anim; //create the animator
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         player = GameObject.FindWithTag("MainCamera").transform;
    15.         anim = this.GetComponent<Animator>(); // set the anim value, we need this or animator will remain empty
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (Vector3.Distance(player.position, this.transform.position) < 10) //testing distance between player and skeleton, if its less than 10
    22.         {
    23.             Vector3 direction = player.position - this.transform.position; //face player
    24.             direction.y = 0; //ignore y axis
    25.             this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f); //rotate to face player.
    26.             if (direction.magnitude < 5 && direction.magnitude > 1) //if we are within range (5)
    27.             {
    28.                 this.transform.Translate(0, 0, 0.05f); //move skeleton to us on z axis (the >1 value above stops it entering into us)
    29.                 anim.SetBool("isAttacking", false); //stop attacking animation
    30.                 anim.SetBool("isIdle", false); //stop idle animation
    31.                 anim.SetBool("isWalking", true); // activate the walking animation
    32.  
    33.             }
    34.             if (direction.magnitude <= 1) // when skeleton reaches us
    35.             {
    36.                 anim.SetBool("isAttacking", true); //attack animation
    37.                 anim.SetBool("isWalking", false);
    38.                 anim.SetBool("isIdle", false);
    39.             }
    40.             if (direction.magnitude >= 5) //if we go out of range
    41.             {
    42.                 anim.SetBool("isIdle", true);
    43.                 anim.SetBool("isWalking", false);
    44.                 anim.SetBool("isAttacking", false);
    45.             }
    46.         }
    47.     }
    48. }
    49.  
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class SpawnManager : MonoBehaviour
    7. {
    8.         public GameObject enemy;
    9.     public float xPos; //base integer
    10.     public float yPos;
    11.     public float zPos;
    12.     public float xPosM; //max range integer
    13.     public float yPosM;
    14.     public float zPosM;
    15.     public int enemyCount;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         StartCoroutine (EnemyDrop());
    20.     }
    21.     IEnumerator EnemyDrop()
    22.     {
    23.         while (enemyCount < 4)
    24.         {
    25.             xPos = Random.Range(xPos, xPosM);
    26.             zPos = Random.Range(zPos, zPosM);
    27.             Instantiate(enemy, new Vector3(xPos, yPos, zPos), Quaternion.identity);
    28.             yield return new WaitForSeconds(1.4f);
    29.  
    30.             enemyCount += 1;
    31.         }
    32.     }
    33. }
    34.