I press play, the enemies spawn in and approachs the player then attacks. But as I move the player, the enemies attack where the player started. PLEASE HELP!
Here's the scripts I use -
Enemy Chase script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chase : MonoBehaviour {
public Transform player;
static Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent();
}
// Update is called once per frame
void Update ()
{
if(Vector3.Distance(player.position, this.transform.position) <2000)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isIdle",false);
if(direction.magnitude > 2)
{
this.transform.Translate(0, 0, 0.05f);
anim.SetBool("isWalking",true);
anim.SetBool("isAttacking",false);
}
else
{
anim.SetBool("isAttacking",true);
anim.SetBool("isWalking",false);
}
}
else
{
anim.SetBool("isIdle", true);
anim.SetBool("isWalking", false);
anim.SetBool("isAttacking", false);
}
}
}
Enemy Spawner script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public Transform[] spawnLocations;
public GameObject[] whatToSpawnPrefab;
public GameObject[] whatToSpawnClone;
// Use this for initialization
void Start ()
{
Spawn();
}
// Update is called once per frame
void Spawn ()
{
whatToSpawnClone[0] = Instantiate(whatToSpawnPrefab[0], spawnLocations[0].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
whatToSpawnClone[1] = Instantiate(whatToSpawnPrefab[1], spawnLocations[1].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
whatToSpawnClone[2] = Instantiate(whatToSpawnPrefab[2], spawnLocations[2].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
}
}
↧