New to c#, any help / feedback on this "ai" code?

Hello!

I am pretty new to using Unity and C#. I am currently trying to make a 3D game with an NPC which goes around the map and if it gets close to the player it will approach him. I have done some code which moves the NPC (Slender, I am trying to make a game like slender for practice) but I am not sure if it is the best way to go about moving it. At the moment it uses “moveTowards” to go to different points on the map which I placed gameObjects at. Can I get any feedback on it or ways to improve? Also any help with moving the NPC towards the player if the player is at a certain range would be appreciated!

Current Code:

using UnityEngine;
using System.Collections;

public class SlenderAI : MonoBehaviour {
    //Setting gameobjects in order to determine position in code
    public GameObject Pos1;
    public GameObject Pos2;
    public GameObject Pos3;
    public GameObject Pos4;
    public GameObject Pos5;
    //Setting float, int, string and array for use in code.
    public float speed = 4.0f;
    private int moving = 0;
    private string goingTo = "";
    private string[] posList = new string[] { "Pos1", "Pos2", "Pos3", "Pos4", "Pos5" };
    // Use this for initialization
    void Start () {

    }
   
    // Update is called once per frame
    void Update () {
        //whatPos = Random string in "posList"
        var whatPos = posList [Random.Range (0, posList.Length - 1)];
        //If slender not moving
        if (moving == 0) {
            Debug.Log ("Moving = 0");
            //If random position is pos 1, set goingTo to Pos1 and moving to false
            if (whatPos == "Pos1") {
               
                goingTo = "Pos1";
                moving = 1;
            }
            if (whatPos == "Pos2") {
                transform.position = Vector3.MoveTowards (transform.position, Pos2.transform.position, speed);
                goingTo = "Pos2";
                moving = 1;
            }
            if (whatPos == "Pos3") {
                transform.position = Vector3.MoveTowards (transform.position, Pos3.transform.position, speed);
                goingTo = "Pos3";
                moving = 1;
            }
            if (whatPos == "Pos4") {
                transform.position = Vector3.MoveTowards (transform.position, Pos4.transform.position, speed);
                goingTo = "Pos4";
                moving = 1;
            }
            if (whatPos == "Pos5") {
                transform.position = Vector3.MoveTowards (transform.position, Pos5.transform.position, speed);
                goingTo = "Pos5";
                moving = 1;
            }
        }
        //If slender isn't already going to a pos
        if (goingTo != null) {
            //If slender is supost to be going to position1
            if (goingTo == "Pos1") {
                //if slender is at position 1
                if (transform.position == Pos1.transform.position) {
                    //Set slender to going nowhere and not moving
                    goingTo = null;
                    moving = 0;
                //else slender isn't at pos1 but is supost to be, so move him to Pos1
                } else {
                    transform.position = Vector3.MoveTowards (transform.position, Pos1.transform.position, speed);
                }
            }

            if (goingTo == "Pos2") {
                if (transform.position == Pos2.transform.position) {
                    goingTo = null;
                    moving = 0;
               
                } else {
                    transform.position = Vector3.MoveTowards (transform.position, Pos2.transform.position, speed);
                }
            }

            if (goingTo == "Pos3") {
                if (transform.position == Pos3.transform.position) {
                    goingTo = null;
                    moving = 0;
                } else {
                    transform.position = Vector3.MoveTowards (transform.position, Pos3.transform.position, speed);
                }
            }


            if (goingTo == "Pos4") {
                if (transform.position == Pos4.transform.position) {
                    goingTo = null;
                    moving = 0;
                } else {
                    transform.position = Vector3.MoveTowards (transform.position, Pos4.transform.position, speed);
                }
            }
            if (goingTo == "Pos5") {
                if (transform.position == Pos5.transform.position) {
                    goingTo = null;
                    moving = 0;
                } else {
                    transform.position = Vector3.MoveTowards (transform.position, Pos5.transform.position, speed);
                }
            }
        }

        //if close to player, move towards player.

        //force camera to look at Slender
        //Activate jumpscare sound
        //-1 life
    }
}

Instead of an array of strings why not make an array of GameObjects for the positions and pick a random one from there? Then goingTo can just become a reference to the current destination and you can get rid of whatPos and checking strings all over the place.

GameObject[] destinations;
GameObject currentDestination;

void Update()
{
    if (currentDestination == null)
        currentDestination = destinations[Random.Range(0, destinations.Length)];

    if (Vector3.Distance(transform.position, currentDestination.transform.position) < 1)
    {
        currentDestination = null;
    }
    else
    {
        trans.position = Vector3.MoveTowards(transform.position, currentDestination.transform.position, speed);
    }
}
1 Like

Nice! Thanks for the help! This seems a lot less chunky and is a great improvement! Thanks so much, didn’t think of doing it that way.

It’s good, as well as the adjustments that @KelsoMRK made.

I recommended using a NavMeshAgent if you’re new to Programming

It’ll allow your objects with AI to move without walking into objects, and it’s really not hard to set up :stuck_out_tongue: