Tutorial Unity C# | A Simple AI (aka, FollowBot)

MatthewH

Member
Messages
1,612
Reaction score
628
I made a YouTube video for this because I find it easier, in my opinion, to explain everything. I encourage you to watch the video, however I will also provide the code below the video.

Code:
// FollowBot (C#) by Coldfire202
// www.youtube.com/ColdfireTube
// www.coldfiretube.com
using UnityEngine;
using System.Collections;

// Make sure to change the 'FollowBotAI' to whatever your file name is
public class FollowBotAI : MonoBehaviour
{
    // Variables for FollowBot
    Transform Leader;
    float AISpeed = 1;
    float MaxDistance = 10;
    float MinDistance = 2;


    // Use this for initialization
    void Start ()
    {
        // Defines who the bot follows
        Leader = GameObject.FindGameObjectWithTag("Player").transform;
    }
  
    // Update is called once per frame
    void Update ()
    {
        transform.LookAt(Leader);
        AI();
    }

    void AI()
    {
        if (Vector3.Distance (transform.position, Leader.position) >= MinDistance)
        {
            transform.position += transform.forward*AISpeed*Time.deltaTime;

            if (Vector3.Distance (transform.position, Leader.position) >= MaxDistance)
            {
                AIDie();
            }
        }
    }

    void AIDie ()
    {
        // Destroys the bot.
        Destroy(gameObject);
    }
}
 

Heisenberg

2 53 34 4 111
Premium
Messages
898
Reaction score
329
Great tutorial, only thing that would have made it better would be if your voice was a bit louder. :smile:
 

Z61

Some times our saints are sinners
Retired
Messages
5,528
Reaction score
3,424
I agree with the above poster, your voice needs to be much louder, even if you just increase it via editing.
 
Top Bottom