Stuck when want to make the AI in Turn-Based Strategy Game

Hai everyone, i already can make the movement grid via pathfinding and avoid obstacles for the player. Right now, i want to make the AI move itself based on the how many movement grid and the action point (just like the player does). But, i don’t know how to do it… Right now, i just able to make the character move to the position (but it is not follow the pathfinding, this character suppose to be the AI)… I am stack at this and try to solve this problem, but couldn’t. Could you guys help me out? Thanks.

Here is the code that i mention in the above (can make the character which is suppose to be AI to move to the position, but it not follow the pathfinding):

using UnityEngine;
using System.Collections;

public class AIPlayer : Player
{
    void Awake()
    {
        moveDestination = transform.position;
    }

	// Use this for initialization
	void Start()
    {
        ColorChanger();
	}
	
	// Update is called once per frame
	void Update()
    {
	
	}
	
	
	public override void TurnUpdate()
	{
        if (GameManager.instance.currentPlayerIndex == 5)
        {
            if (Vector3.Distance(moveDestination, transform.position) > 0.1f)
            {
                transform.position += (moveDestination - transform.position).normalized * moveSpeed * Time.deltaTime;

                if (Vector3.Distance(moveDestination, transform.position) <= 0.1f)
                {
                    transform.position = moveDestination;
                    actionPoints--;
                }
            }

            else
            {
                moveDestination = new Vector3(2 - Mathf.Floor(GameManager.instance.mapSize / 2), 1.5f, -2 + Mathf.Floor(GameManager.instance.mapSize / 2));
                GameManager.instance.NextTurn();
            }
        }

        base.TurnUpdate();
	}

    public override void TurnOnGUI()
    {
        
    }

    public override void ColorChanger()
    {
        base.ColorChanger();
    }
}

Here is the link video for the game:

Just a quick tip: it's "stuck", not "stack". Worth getting this right, particularly in this context, as "stack" is a type of data structure, and a feature of most execution environments, which could be quite confusing.

1 Answer

1

Late answer … but I’m looking at this myself so I’ll write what I know down for others. AI is a big topic.

First, I’d have a look at something like RAIN (free from the asset store) to see whether it does what you want.

Failing that, take a look at the path finding algorithms: A*, Dijkstra’s and floodfill. There’s code that does this already you can download. A fellow called UnityChat has got a good tutorial for using the A* algorithm in turn based strategy games on youtube.

Then you may want to build some AI on top of your raw pathfinding with some kind of planning code. (That’s as far as I’ve got so far :slight_smile: