Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I make my enemy chase the player?

Discussion in 'Scripting' started by JulianVk, Jun 16, 2014.

  1. JulianVk

    JulianVk

    Joined:
    Apr 16, 2014
    Posts:
    3
    Hello,

    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enemy : Entity {
    5.  
    6.     public float expOnDeath;
    7.     private Player player;
    8.  
    9.     void Start() {
    10.         player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
    11.     }
    12.  
    13.  
    14.     public override void Die () {
    15.         player.AddExperience(expOnDeath);
    16.         base.Die ();
    17.     }
    18. }
    19.  
    Player script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : Entity {
    5.  
    6.     private int level;
    7.     private float currentLevelExperience;
    8.     private float experienceToLevel;
    9.  
    10.     void Start() {
    11.         LevelUp ();
    12.     }
    13.  
    14.  
    15.     public void AddExperience(float exp) {
    16.         currentLevelExperience += exp;
    17.         if (currentLevelExperience >= experienceToLevel) {
    18.             currentLevelExperience -= experienceToLevel;
    19.             LevelUp();
    20.         }
    21.      
    22.         Debug.Log("EXP: " + currentLevelExperience + "   Level: " + level);
    23.     }
    24.  
    25.     private void LevelUp() {
    26.         level++;
    27.         experienceToLevel = level * 50 + Mathf.Pow(level * 2,2);
    28.      
    29.         AddExperience(0);
    30.     }
    31. }
    32.  
    Entity script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Entity : MonoBehaviour {
    5.    
    6.     public float health;
    7.    
    8.    
    9.     public virtual void TakeDamage(float dmg) {
    10.         health -= dmg;
    11.        
    12.         if (health <= 0) {
    13.             Die();
    14.         }
    15.     }
    16.    
    17.     public virtual void Die() {
    18.         Destroy(gameObject);
    19.     }
    20. }
    21.  
     
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    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:

    Code (csharp):
    1. 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.
     
  3. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    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)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAI : MonoBehaviour {
    5.     public Transform player;
    6.     public NavMeshAgent agent;
    7.  
    8.     void Start () {
    9.         if (player == null) {
    10.             player = GameObject.FindGameObjectWithTag("Player").transform;
    11.         }
    12.         if (agent == null) {
    13.             agent = GetComponent<NavMeshAgent>();
    14.         }
    15.     }
    16.  
    17.     void FixedUpdate () {
    18.         agent.SetDestination(player.position);
    19.    
    20.         //Kills player when distance is below 1, you wanted it to be 0.
    21.         //but that would man that the enemy is inside of the player, at the exact same position o_o
    22.         if(Vector3.distance(transform.position, player.position < 1) {
    23.             //player.GetComponent<Player>().Kill();
    24.         }
    25.     }
    26. }
     
  4. JulianVk

    JulianVk

    Joined:
    Apr 16, 2014
    Posts:
    3
    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.

     
    Last edited: Jun 19, 2014
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    That was a mistake on my part, sorry for that.
    line 22:
    Code (CSharp):
    1. if(Vector3.distance(transform.position, player.position) < 1) {
     
  6. Asherhero

    Asherhero

    Joined:
    Aug 8, 2019
    Posts:
    5
    hey man, I can make my enemy run and hide but not shoot.