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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

A.I. targetting troubles

Discussion in 'Navigation' started by MorganLee909, Sep 10, 2015.

  1. MorganLee909


    Aug 7, 2015
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    4. public class Zombie : MonoBehaviour {
    5.     public GameObject target;
    6.     public NavMeshAgent agent;
    7.     public float zombieSpeed = 2.2f;
    8.     public Animator animator;
    9.     public float zombieDamage = 15f;
    11.     private CharacterController controller;
    12.     private Player player;
    13.     private bool foundPlayer = false;
    14.     private float distanceToPlayer;
    16.     void Start(){
    17.         agent = GetComponent<NavMeshAgent> ();
    18.         controller = GetComponent<CharacterController> ();
    19.     }
    21.     void Update () {
    22.         distanceToPlayer = Vector3.Distance (transform.position, target.transform.position);
    23.         if (!foundPlayer) {
    24.             Walking ();
    25.         }else if(distanceToPlayer >= 5f){
    26.             foundPlayer = false;
    27.             animator.SetBool("isAttacking", false);
    28.         }
    29.     }
    31.     void Walking (){
    32.         agent.SetDestination (target.transform.position);
    33.         Vector3 moveDirection = (transform.position - target.transform.position) / distanceToPlayer;
    34.         controller.Move (moveDirection * zombieSpeed * Time.deltaTime);
    35.     }
    37.     public void OnControllerColliderHit(ControllerColliderHit collider){
    38.         if (collider.collider.GetComponent<Player> ()) {
    39.             foundPlayer = true;
    40.             animator.SetBool("isAttacking", true);
    41.             player.TakeDamage(zombieDamage);
    42.         }
    43.     }
    44. }
    New programmer here and I am having some trouble with making a zombie move toward toward the player in the game that I am creating. The trouble that I am having is that when I drag the player prefab into the public Transform in the inspector, it will search out the original position of the prefab but not the player as he moves. However, when I use the player from the scene, then as the zombies spawn they will not have the player set in the public Transform. I hope I explained this sufficiently well if not then let me know. Anybody know how to solve this?
  2. Rostam24


    Mar 5, 2014
    I assume there is only 1 player? If so, you can use a static 'singleton' to make things easier for yourself.

    The player has a public static player field. In the Awake() method, you set: player = this. Then you can use the static reference to the player object.

    Googling for singleton unity gives this hit, which can help you:
    MorganLee909 likes this.
  3. JamesLeeNZ


    Nov 15, 2011
    never recommend static to a new programmer. Its a cluster f*** waiting to happen.

    You need to have an instance of the player in the scene and drag that value into your target field. If youve done that, there is no reason your code shouldnt work. To me it just sounds like youve dragged the player prefab from your library.

    Are you moving the zombie or are you expecting the agent to move it?

    Seeing as you seem to be driving the character controller, whats the point of the agent? You're not using its path best I can tell unless character controller has some built in code to do it.
    MorganLee909 likes this.