Search Unity

"Enemy Patrol and Sight" C#

Discussion in 'Scripting' started by Legosdoctor, Sep 4, 2015.

  1. Legosdoctor

    Legosdoctor

    Joined:
    Jul 23, 2015
    Posts:
    21
    So some people helped me with the AI, I need the enemy to be able to navigate through a maze, and when in a certain distance from the player, speed will change, Heres the script I have

    usingUnityEngine;
    usingSystem.Collections;

    publicclassAI : MonoBehaviour {

    publicTransformPlayer;
    publicfloatmoveSpeed = 2.0f;
    NavMeshAgentagent;

    voidStart ()
    {
    agent = GetComponent<NavMeshAgent> ();
    }

    voidUpdate(){
    transform.LookAt(Player);

    transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

    agent.SetDestination (Player.position);
    }
    }

    Anything that has to be added, i can add, just tell me how to get it to patrol!
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Patrol is straight forward. Just set a series of way points and have the Navmesh agent navigate to each one in turn. To patrol just loop back to the beginning of the list once all the way points are visited.
     
  3. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    I would recommend watching some of the related videos in the Stealth tutorial:
    http://unity3d.com/learn/tutorials/projects/stealth-tutorial-4x-only

    Note: This tutorial, overall, doesn't translate to Unity 5, as I understand it, but it should be good reference material for enemy sight scripts and AI. Just watching the vids and reading the scripts under the Enemy Guards section might be useful to what you are looking at.