- Home /
Question by
DNA10 · Aug 11, 2020 at 10:15 AM ·
enemy aienemiesenemy spawn
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");
}
}
Comment
Your answer
Follow this Question
Related Questions
Setting up a wave system 1 Answer
Enemy AI Script He's Going in the wrong direction 0 Answers
Different enemy types script design 1 Answer
Asset for enemies behaviour management 0 Answers