Hi guys, im working on a game where you’re a predatory fish hunting herbivore fish on a 2D top down plane.
I want my enemies to chase the nearest objects with the tag “food” but the problem is, they don’t. They all simultaneously (wherever they spawn) choose one random food object on the map to chase and ignore all other food objects (even if it is right next to them) until they reach their target…
Here’s my code:
using UnityEngine;
using System.Collections;
public class EnemyScript : MonoBehaviour {
Transform target;
public float speed = 1;
void Start () {
//target = GameObject.FindGameObjectWithTag("Player").transform; //my predator
target = GameObject.FindGameObjectWithTag("Food").transform; //my prey
}
void Update(){
transform.LookAt (transform.position + new Vector3 (0, 0, 1), transform.position - target.transform.position); //Face target
//transform.LookAt (transform.position + new Vector3 (0, 0, 1), target.transform.position - transform.position); //Face away from target
transform.Translate(Vector3.down * speed * Time.deltaTime); //movement forwards
}
}
Thanks guys any help would really be appreciated.