A* Pathfinding Project

AIDestinationSetter working with a prefab Player. Is there an alternative way to set destination?


#1

I’m new to both Unity and A* Pathfinding Project. So I’m sorry if this is an overly simple problem to solve. I feel i’m approaching the solution incorrectly.

Within the code of AIDestinationSetter.cs there is a line:

public Transform target;

This doesn’t work correctly with the Player being a prefab. As you can’t link gameobjects in a scene with a prefab.

I’m wondering if that line of code can be changed so that it will get the transform of a gameobject within the scene that it already knows the name of. No longer having it be drag and dropped in.

I would then have the same gameobject in every scene I create so that the Player prefab can find it. Is this the correct approach?

I want to make my Player a prefab for ease of learning with other assets. When the player isn’t a prefab, and is just a gameobject in the scene the pathfinding works perfectly.

I tried also making the Target transform into a prefab as well which allowed me to link its location to the Player prefab. However on game load the player will walk to the target location but it can’t be moved with mouse click anymore and will only use originally set location.

Maybe there is a whole new approach to this that I haven’t thought of or came across.

Your help would be appreciated.

Thank you all.


#2

Hey.

You can always just set the destination yourself through your own script.
https://arongranberg.com/astar/docs/aibase.html#destination


#3

Thank you for your reply :slightly_smiling_face:

Had a look at the link you sent with my friend and using that information we replaced the AIDestinationSetter.cs code with the code below. We basically merged the TargetMover.cs and AIDestinationSetter.cs together and everything works perfectly.

using UnityEngine;
using System.Collections;

namespace Pathfinding
{
    [UniqueComponent(tag = "ai.destination")]
    public class AIDestinationSetter : VersionedMonoBehaviour
    {
        IAstarAI ai;
        Camera cam;

        public void Start()
        {
            //Cache the Main Camera
            cam = Camera.main;
            useGUILayout = false;
        }

        void OnEnable()
        {
            ai = GetComponent<IAstarAI>();
        }

        // Handles onClick
        public void OnGUI()
        {
            if (cam != null && Event.current.type == EventType.MouseDown && Event.current.clickCount == 1)
            {
                UpdateTargetPosition();
            }
        }

        public void UpdateTargetPosition()
        {
            Vector3 newPosition = Vector3.zero;
            bool positionFound = false;

            newPosition = cam.ScreenToWorldPoint(Input.mousePosition);
            newPosition.z = 0;
            positionFound = true;

            if (positionFound && newPosition != ai.destination)
            {
                if (ai != null) ai.destination = newPosition;
            }
        }
    }
}


#4

Hi

Excellent. However I’d recommend to use a new script outside the pathfinding package instead of modifying the AIDestinationSetter. Otherwise your changes may be overriden if you upgrade the package in the future.