NoobProgrammer
NoobProgrammer

Reputation: 61

Prefab shares the same animation

Okay, I have a gameobject that clones a prefab programmatically (like a tower defense game that spawns enemies), the prefab has an animator controller attached to it. My problem is that whatever animation is playing from 1 prefab, the others share the same animation. For example, one prefab is in attack animation the others will do the same animation, and I do not want that.

Here is the script of the prefab:

using System;
using UnityEngine;
using Random = UnityEngine.Random;


public class AICharacterControl : MonoBehaviour
{
    public UnityEngine.AI.NavMeshAgent agent { get; private set; }                   
    [SerializeField] private GameObject[] GameObjects;                                    // target to aim for
    Transform target;
    Animator animator;
    int velocity = 0;
    Rigidbody rb;
    LayerMask layer;
    Collider collider;     
    private GameObject player;
    private Transform playerPosition;
    bool isMoving;

void Start()
    {

        animator = GetComponent<Animator>();
        rb = GetComponent<Rigidbody>();
        player = GameObject.FindGameObjectWithTag("Player");

        agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
        int n = Random.Range(0, GameObjects.Length);
        target = GameObjects[n].GetComponent<Transform>();

        agent.SetDestination(target.position);
        playerPosition = player.GetComponent<Transform>();



}


void Update()
    {

        if (Vector3.Distance(playerPosition.position, this.transform.position) < 10 && Vector3.Distance(playerPosition.position, this.transform.position) > 5 && player.transform.position.y < 6)
        {
        animator.SetBool("isMoving", true);
        agent.SetDestination(playerPosition.position);

            if (Vector3.Distance(playerPosition.position, this.transform.position) <= 5)
            {

                animator.SetBool("attack", true);

            }


        }

        else if (playerPosition.transform.position.y > 6)
        {

            agent.SetDestination(target.position);
            animator.SetBool("attack", false);

            if (agent.remainingDistance > agent.stoppingDistance)
            {

                animator.SetBool("isMoving", true);
                if (agent.remainingDistance <= 3)
                {

                    isMoving = false;


                }

            }


        }  

    }

This is the script of the game object that spawns the prefab:

public class SpawnScript : MonoBehaviour {

// Use this for initialization
public GameObject NavAgent;
public int TotalSpawned = 0;
public int spawnDelay = 2;
public int SpawnQuantity;
public int SpawnRemaining;
public bool invoke = true;
private float time;
void Start () {

    SpawnRemaining = SpawnQuantity;

}

// Update is called once per frame
void Update() {

    time += Time.deltaTime;
    while (invoke == true && time >= spawnDelay)
    {
        GameObject spawner = Instantiate(NavAgent, this.transform.position, Quaternion.identity);     
        Invoke("SpawnAgent", spawnDelay);
        TotalSpawned++;
        time = 0;

        if (TotalSpawned == SpawnQuantity)
        {
            SpawnRemaining--;
            invoke = false;
        }

    }



}

Upvotes: 0

Views: 523

Answers (1)

D&#225;vid Florek
D&#225;vid Florek

Reputation: 571

The problem is in if (playerPosition.transform.position.y > 6). It checks if player's y position is greater than 6 and than attacks. Player's position is same across all the prefabs, you need to compare distance between player and prefab.

Upvotes: 0

Related Questions