Kaoru
Kaoru

Reputation: 2883

Stuck trying to make the AI in Turn-Based Strategy Game

I can already make the movement grid via pathfinding and avoid obstacles for the player. Now I want to make the AI move itself based on the movement grid and available action points (just like the player does), but I don't know how to do it. Right now, I am only 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 stuck trying to solve this problem, but couldn't. Could you guys help me out? Thanks.

Here is the code I've got so far (it makes 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:

http://www.youtube.com/watch?v=eo7DzbBPQBs&feature=youtu.be

Upvotes: 2

Views: 2178

Answers (1)

nerdyaswild
nerdyaswild

Reputation: 122

There's no code for pathfinding there. It's just an overly complicated lerp. As well, moving directly to the position will cause major headaches with graphical obstacle avoidance.

Your best bet is to implement a grid-based navmesh and use something like an A* search to find the path. The movement would be limited to adjacent squares, so graphical obstacle avoidance wouldn't happen.

Have a look at this tutorial. It'll give you everything you need except for animating player movement and the game logic for the target position.

Upvotes: 1

Related Questions