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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Simple Follower AI HELP C#

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

  1. Legosdoctor

    Legosdoctor

    Joined:
    Jul 23, 2015
    Posts:
    21
    So, I know people don't like give-me threads, but I've tried everything, Does anyone have a simple AI script that makes the "enemy" follow the player!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    goonter likes this.
  3. goonter

    goonter

    Joined:
    Aug 31, 2015
    Posts:
    89
    Try Transform.LookAt and transform.Translate(Vector3.forward * Time.deltaTime). This is extremely simple though. If you want it to use the navmesh you use NavMeshAgent.SetDestination(player.position) and just constantly update SetDestination to the player's current position.
     
  4. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Simplest way to do this is make the enemy look at the player and move towards it at a certain speed :
    Code (CSharp):
    1. public Transform target; //the player
    2. public float moveSpeed = 2.0f;
    3.  
    4. void Update(){
    5. //Look at the target
    6. transform.LookAt(target);
    7.  
    8. //Move towards the target
    9. transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    10. }
    Check out the Learn Section. The Stealth project has some good AI that you can learn from.
     
  5. Legosdoctor

    Legosdoctor

    Joined:
    Jul 23, 2015
    Posts:
    21
    So, How do i make the script see the target as the player?
     
  6. Legosdoctor

    Legosdoctor

    Joined:
    Jul 23, 2015
    Posts:
    21
    WAit, NVM i figured it out, Thanks a lot m8