When I activate the animator of the enemy and play it spawns at the corner of the map and don't move
When I play my game without the animations in the enemies they spawn randomly inside the determined area, but turned 90 degrees to the side (half of it stays inside the map). And when the animations are activated they spawns outside the map, in a corner, and don't move (i made a script to make them chase the player). It's my first game with Unity hope someone could help me with this bug.
Enemy Generation Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateEnemies : MonoBehaviour
{
public GameObject theEnemy;
public int xPos;
public int zPos;
public int enemyCount;
void Start()
{
StartCoroutine(EnemyDrop());
}
IEnumerator EnemyDrop()
{
while(enemyCount < 10)
{
xPos = Random.Range(100, 200);
zPos = Random.Range(100, 200);
Instantiate(theEnemy, new Vector3(xPos, 1, zPos), Quaternion.identity);
yield return new WaitForSeconds(0.1f);
enemyCount += 1;
}
}
}
Enemy AI script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyController : MonoBehaviour
{
public float lookRadius = 10f;
public float attackDistance = 2f;
public Animator anima;
bool isDead = false;
Transform target;
NavMeshAgent agent;
void Start()
{
target = PlayerManager.instance.player.transform;
agent = GetComponent<NavMeshAgent>();
anima = GetComponent<Animator>();
}
void Update()
{
float distance = Vector3.Distance(transform.position, target.position);
if (distance <= lookRadius && !isDead)
{
ChasePlayer();
Debug.Log(transform.position);
}
if (distance <= attackDistance && !isDead)
{
AttackPlayer();
}
if (distance > lookRadius && !isDead)
{
anima.SetBool("Moving", false);
anima.SetBool("Attacking", false);
}
}
public void EnemyDeathAnim()
{
isDead = true;
anima.SetTrigger("isDead");
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
void ChasePlayer()
{
//agent.updatePosition = true;
agent.SetDestination(target.position);
anima.SetBool("Moving", true);
anima.SetBool("Attacking", false);
}
void AttackPlayer()
{
//agent.updatePosition = false;
anima.SetBool("Moving", false);
anima.SetBool("Attacking", true);
Debug.Log("attack");
}
}
Your answer
Follow this Question
Related Questions
Unity2D Need Help in Multiple Enemies Shooting Script 0 Answers
How to make all enemies attack after spawning? Not only 1. 1 Answer
Glitched character problem - Urgent Help Needed! 1 Answer
Respawing Enemy After GameObject is Destroyed 2 Answers
Multiple AI enemies: Using OnTrigger for "line of sight" activates all enemies at once 2 Answers