I’m a beginner and I would like some help with this subject. I’m making a top down shooter (using all the same scripts and animations from this playlist https://www.youtube.com/playlist?list=PLFt_AvWsXl0ct75VmGXskJnzrStEbPD6d) and right now I’ve already got a moving player and a non-moving enemy. How do I make the enemy chase the player? After this I want the player to do die instantly when the distance between the enemy and player is 0. Once again I’m a newbie so please go easy on me. Any help would be appreciated.
Enemy script so far:
using UnityEngine;
using System.Collections;
public class Enemy : Entity {
public float expOnDeath;
private Player player;
void Start() {
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
}
public override void Die () {
player.AddExperience(expOnDeath);
base.Die ();
}
}
Player script:
using UnityEngine;
using System.Collections;
public class Player : Entity {
private int level;
private float currentLevelExperience;
private float experienceToLevel;
void Start() {
LevelUp ();
}
public void AddExperience(float exp) {
currentLevelExperience += exp;
if (currentLevelExperience >= experienceToLevel) {
currentLevelExperience -= experienceToLevel;
LevelUp();
}
Debug.Log("EXP: " + currentLevelExperience + " Level: " + level);
}
private void LevelUp() {
level++;
experienceToLevel = level * 50 + Mathf.Pow(level * 2,2);
AddExperience(0);
}
}
Entity script:
using UnityEngine;
using System.Collections;
public class Entity : MonoBehaviour {
public float health;
public virtual void TakeDamage(float dmg) {
health -= dmg;
if (health <= 0) {
Die();
}
}
public virtual void Die() {
Destroy(gameObject);
}
}
I’m not sure how you’re moving your player/character (whether they’re rigidbodies, character controllers, etc), but to get the direction between the player and the enemy you can use:
Vector3 direction = (player.gameObject.transform.position - transform.position).normalized;
You can then rotate the enemy to face the player and move towards him using Quaternion.LookRotation.
Use NavMesh to make the enemies walk towards to player with pathfinding.
Here’s a script that will point you into the right direction:
(Put this script on the enemy)
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform player;
public NavMeshAgent agent;
void Start () {
if (player == null) {
player = GameObject.FindGameObjectWithTag("Player").transform;
}
if (agent == null) {
agent = GetComponent<NavMeshAgent>();
}
}
void FixedUpdate () {
agent.SetDestination(player.position);
//Kills player when distance is below 1, you wanted it to be 0.
//but that would man that the enemy is inside of the player, at the exact same position o_o
if(Vector3.distance(transform.position, player.position < 1) {
//player.GetComponent<Player>().Kill();
}
}
}
Thank you for your response. Though I’ve encountered another error. I’ve attached the enemy NavMeshAgent and the Player to the EnemyAI script but when I try to run it, it says: “SetDestination can only be called on an active agent that has been placed on a NavMesh.” (line 18). Any idea how to fix this? EDIT: Fixed.
Next question: I am able to shoot through my walls but I don’t want this. How do I stop this?
Also I get a parsing error at the last 3 accolades.