Need assistance with an primitive AI.
I'm trying to finish a project in one of my classes, but the bug script I've created is not working(Used to work before I added new protocols). I'm trying to make it so that a float, variable, or some form of Array holds account of all objects with the tag "Crop", during this process it should gather their transforms and then sort out all "Crop" distances, then have the script select the closest tagged GameObject to charge at. I have tried all I can from knowledge and gathered knowledge, but I can't figure this problem out.
using UnityEngine;
using System.Collections;
public class BugAI : MonoBehaviour {
public float fpsTargetDistance;
public float enemyLookDistance;
public float attackDistance;
public float enemyMovementSpeed;
public float damping;
public float[] targets;
public Transform selectedTarget;
Rigidbody theRigidbody;
public float[] List;
// Use this for initialization
void Start () {
theRigidbody = GetComponent<Rigidbody> ();
selectedTarget = null;
foreach (float target in targets) {
AddAllEnemys ();
}
}
public void AddAllEnemys(){
float go = GameObject.FindGameObjectsWithTag ("Crop");
foreach (float enemy in go) {
AddTarget (enemy.GetTypeCode(Transform));
}
}
public void AddTarget (Transform enemy)
{
var Amm = targets [0];
Amm = new float(List[0] + 1);
}
private void SortTargetsByDistance()
{
targets.Sort (delegate(Transform t1, Transform t2) {
return Vector3.Distance (t1.position, transform.position).CompareTo (Vector3.Distance (t2.position, transform.position));
});
}
private void TargetEnemy()
{
var Kel = 0;
if (selectedTarget == null) {
SortTargetsByDistance ();
selectedTarget = targets [0];
} else {
Kel = 1;
}
selectedTarget = targets [Kel];
}
// Update is called once per frame
void FixedUpdate () {
fpsTargetDistance = Vector3.Distance (selectedTarget.position, transform.position);
if (fpsTargetDistance < enemyLookDistance) {
lookAtPlayer();
TargetEnemy ();
print ("Look at Playa please!");
}
if (fpsTargetDistance < attackDistance) {
attackPlease ();
print ("ATTACK!");
}
else {
print ("sad face");
}
}
void lookAtPlayer () {
if (fpsTargetDistance > attackDistance) {
Quaternion rotation = Quaternion.LookRotation (selectedTarget.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * damping);
theRigidbody.AddForce (transform.forward*enemyMovementSpeed/2);
} else {
Quaternion rotation = Quaternion.LookRotation (selectedTarget.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * damping);
}
}
void attackPlease () {
theRigidbody.AddForce (transform.forward*enemyMovementSpeed);
}
}
Your answer
Follow this Question
Related Questions
How do I find a player for AI to target when spawning a player in? 0 Answers
How to get the initial position of all gameobjects in an array unity 1 Answer
How to change a game objects tag after it has been randomly selected? 0 Answers
Array transform.position from another script 1 Answer
Enemy Patrol AI is sideways 0 Answers