Aron Greenberg wrote a wonderful framework for Unity 3D to let a character or any other gameobject / transform follow a path or move from one point to another.

We used this Unity package for our game Erwin’s timewarp which you can find on steam greenlight currently (BTW: we need your votes;-)):

The A* pathfinding framework is used for the AI of characters in the game and also for the main character, the dog Erwin. Normally Erwin is controlled with the keyborad or game controller, but when he notices an object which he can interact with, the player can click the Use- or Pick-action and Erwin is walking towards the object, stops in front of it and then does an interaction-animation like picking or whatever:

As you can see in the video, Erwin is facing the object he wants to use while he is moving to a special point in front of the object. This logic is done by using some parts of the AIPath-class of the A* project.

Setup of Characters

For the housekeeper in our game we assigned a CharacterController-component and 2 waypoints he is moving towards, but when the dog Erwin is near to him he is following and tries to hit him with a baseball bat (not nice):

This is the C# code (housekeeper.cs) to let the housekeeper follow the player when the distance is less than 5.0f:

using UnityEngine;
using System.Collections;

namespace Pathfinding
{
    [RequireComponent(typeof(Seeker))]
    public class Housekeeper : AIPath, IHitable
    {
        private Animator _animator = null;

        protected override void Awake()
        {
            base.Awake();

            _animator = GetComponent<Animator>();
        }

        // Use this for initialization
        protected override void Start()
        {
            base.Start();
        }


        public override Vector3 GetFeetPosition()
        {
            return tr.position;
        }


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

        // Update is called once per frame
        protected new void Update()
        {

            //Get velocity in world-space
            Vector3 velocity;
            if (canMove)
            {
                // Calculate desired velocity
                Vector3 dir = CalculateVelocity(GetFeetPosition());

                // Rotate towards targetDirection (filled in by CalculateVelocity)
                RotateTowards(targetDirection);

                dir.y = 0;

                if (dir.sqrMagnitude <= sleepVelocity * sleepVelocity)
                {
		   dir = Vector3.zero;
                }

                if (navController != null)
                {
                    velocity = Vector3.zero;
                }
                else if (controller != null)
                {
                    controller.SimpleMove(dir);
                    velocity = controller.velocity;
                }
                else
                {
                    Debug.LogWarning("No NavmeshController or CharacterController attached to GameObject");
                    velocity = Vector3.zero;
                }

                Vector3 relVelocity = tr.InverseTransformDirection(velocity);
                relVelocity.y = 0;


                // Distance to target (player) calculation
                float dist = Vector3.Distance(PlayerController.Instance.transform.position, tr.position);

                if (dist < 5.0f)
                {
                    target = PlayerController.Instance.PlayerTransform;

		    _animator.SetBool("dowalk", false);                        
                }
                else
                {
                    _animator.SetBool("dowalk", true);
                }

                if (relVelocity.sqrMagnitude <= sleepVelocity * sleepVelocity)
                {
                    _animator.SetBool("dowalk", false);
                }
            }
            else
            {
                velocity = Vector3.zero;
            }
        }
    }
}

The character needs the following components to work with A* pathfinding:

A* pathfinding components

A* pathfinding components

  • A Character-controller (or Navmesh or Rigidbody)
  • A seeker component (aprt of A* framework)
  • An animator (if you need animations)
  • A script like the housekeeper.cs (class derived from AIPath)
  • An A* Gameobject has to be placed to the scene (part of the A* framework)

And that’s it to use A* pathfinding in your Unity project.

(Visited 12 times, 1 visits today)