- Home /
Pathfinding AI with Aron Granberg, Prefabs with AI not Retaining Target Transform, Trying to change for AI to Follow GameObjects with Tag "Player" PLZ HELP
I'm making a top down shooter where zombies have to follow the player and waves of zombies come through the windows similar to COD:zombies. Whenever I have the prefab inspector tool open in unity however it doesn't let me set the player as a target. It only allows me to do so after the zombies have spawned in. I think setting the AI to follow a tag would fix it but I'm fairly new to coding. Any help?
//My Code That Doesn't Work
using UnityEngine;
using System.Collections;
namespace Pathfinding {
/// <summary>
/// Sets the destination of an AI to the position of a specified object.
/// This component should be attached to a GameObject together with a movement script such as AIPath, RichAI or AILerp.
/// This component will then make the AI move towards the <see cref="target"/> set on this component.
///
/// See: <see cref="Pathfinding.IAstarAI.destination"/>
///
/// [Open online documentation to see images]
/// </summary>
[UniqueComponent(tag = "ai.destination")]
[HelpURL("http://arongranberg.com/astar/docs/class_pathfinding_1_1_a_i_destination_setter.php")]
public class AIDestinationSetter : VersionedMonoBehaviour {
/// <summary>The object that the AI should move to</summary>
public Transform target;
IAstarAI ai;
void OnEnable () {
ai = GetComponent<IAstarAI>();
// Update the destination right before searching for a path as well.
// This is enough in theory, but this script will also update the destination every
// frame as the destination is used for debugging and may be used for other things by other
// scripts as well. So it makes sense that it is up to date every frame.
if (ai != null) ai.onSearchPath += Update;
}
void OnDisable () {
if (ai != null) ai.onSearchPath -= Update;
}
/// <summary>Updates the AI's destination every frame</summary>
void Update ()
{
player = GameObject.FindWithTag("Player").transform;
if (player != null && ai != null)
{
ai.destination = player.position;
}
}
}
}
// The Original
sing UnityEngine;
using System.Collections;
namespace Pathfinding {
/// <summary>
/// Sets the destination of an AI to the position of a specified object.
/// This component should be attached to a GameObject together with a movement script such as AIPath, RichAI or AILerp.
/// This component will then make the AI move towards the <see cref="target"/> set on this component.
///
/// See: <see cref="Pathfinding.IAstarAI.destination"/>
///
/// [Open online documentation to see images]
/// </summary>
[UniqueComponent(tag = "ai.destination")]
[HelpURL("http://arongranberg.com/astar/docs/class_pathfinding_1_1_a_i_destination_setter.php")]
public class AIDestinationSetter : VersionedMonoBehaviour {
/// <summary>The object that the AI should move to</summary>
public Transform target;
IAstarAI ai;
void OnEnable () {
ai = GetComponent<IAstarAI>();
// Update the destination right before searching for a path as well.
// This is enough in theory, but this script will also update the destination every
// frame as the destination is used for debugging and may be used for other things by other
// scripts as well. So it makes sense that it is up to date every frame.
if (ai != null) ai.onSearchPath += Update;
}
void OnDisable () {
if (ai != null) ai.onSearchPath -= Update;
}
/// <summary>Updates the AI's destination every frame</summary>
void Update ()
{
if (target != null && ai != null)
{
ai.destination = target.position;
}
}
}
}
Answer by Snubber · May 25, 2021 at 06:01 PM
The prefab inspector tool is only used for editing the prefab file which has not been loaded into the scene yet. Because you're editing a zombie that is not loaded into the scene yet there is no way for it to know about the player which exists within the scene.
Your idea to search for player based on the tag should work. Just make sure that the player actually has the tag. You could also use Object.FindObjectsOfType and put in the type that is whatever Component your player has.
If you are new to coding you might not have used the debugger yet. I highly recommend trying it. It allows you to pause the code at a certain point and see what values certain variables have. For example, after the line player = GameObject.FindWithTag("Player").transform
you can pause the code and check to make sure that a player is actually found. Each code editor has a different way to attach the debugger: https://docs.unity3d.com/Manual/ManagedCodeDebugging.html