How to use brackeys ENEMY AI 2d Platformer tutorial script modifed as prefab.
When i make Enemy a prefab it’s “public Transformer target” is unable to assign from instructor,
when i do it in script dont work target = GameObject.FindGameObjectWithTag(“car”).transform.
Need Help
using UnityEngine;
using System.Collections;
using Pathfinding;
[RequireComponent (typeof(Seeker))]
[RequireComponent (typeof(Rigidbody2D))]
public class EnemyAI : MonoBehaviour {
// Use this for initialization
//what to chase?
public Transform target;
private GameObject c;
//each sec update path
public float updateRate = 2f;
// catching
private Seeker seeker;
private Rigidbody2D rb;
//The calculated path
public Path path;
//AI speed
public float speed = 300f;
public ForceMode2D fMode;
[HideInInspector]
public bool pathIsEnded = false;
//Max distance from AI
public float nextWaypointDistance = 3;
//waypoint we are moving towards
private int currentWaypoint = 0;
private bool searchingForPlayer = false;
private float maxSqrDistance = 16; //4 meters
private float Range = 100;
private GameObject car;
void Start () {
c = GameObject.FindGameObjectWithTag(“car”); //problem occurs here
target = c.transform; //target remains null
seeker = GetComponent ();
rb = GetComponent ();
if (target == null) {
//Debug.LogError(“No player”);
if(!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
}
return;
}
//strat new path
seeker.StartPath (transform.position,target.position,OnPathComplete);
StartCoroutine (UpdatePath ());
}
void Awake()
{
target = GameObject.FindGameObjectWithTag (“car”).transform;
if (target == null) {
//Debug.LogError(“No player”);
if(!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
}
return;
}
}
IEnumerator SearchForPlayer()
{
GameObject sResult = GameObject.FindGameObjectWithTag(“car”);
if(sResult == null)
{
yield return new WaitForSeconds(0.5f);
StartCoroutine(SearchForPlayer());
}
else{
target = sResult.transform;
StartCoroutine(UpdatePath());
searchingForPlayer = false;
}
}
IEnumerator UpdatePath()
{
if (target == null) {
//Debug.LogError(“No player”);
if(!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
}
return false;
}
//start new path
seeker.StartPath (transform.position,target.position,OnPathComplete);
yield return new WaitForSeconds (1f / updateRate);
StartCoroutine (UpdatePath ());
}
public void OnPathComplete(Path p)
{
Debug.Log (“path error” + p.error);
if (!p.error) {
path = p;
currentWaypoint = 0;
}
}
void OnTriggerEnter2D(Collider2D collider)
{
Debug.Log(“trigger”);
if (collider.gameObject.name == “car”)
{
car = GameObject.FindGameObjectWithTag(“car”);
Destroy(car);
}
}
// Update is called once per frame
void FixedUpdate () {
//Range = Vector2.Distance (target.position, rb.transform.position);
Range = 14;
//Debug.Log (Range);
if (target == null) {
//Debug.LogError(“No player”);
if(!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
}
return;
}
if (path == null) {
return;
}
if (currentWaypoint >= path.vectorPath.Count) {
if(pathIsEnded)
return;
Debug.Log(“End of Path”);
pathIsEnded = true;
return;
}
pathIsEnded = false;
//dir of next waypoint
Vector3 dir = (path.vectorPath [currentWaypoint] - transform.position).normalized;
dir *= speed * Time.fixedDeltaTime;
//move AI
rb.AddForce (dir, fMode);
float dist = Vector3.Distance (transform.position, path.vectorPath [currentWaypoint]);
if (dist < nextWaypointDistance && Range < maxSqrDistance) {
currentWaypoint++;
return;
}
}
}
You may have better luck getting an answer to this on the Brackeys Forums at: http://forum.brackeys.com/. Almost nobody is going to watch the video series and search through them just to answer your question, and the chances that somebody has seen the video already and is going to pop in here and help you are slim.