- Home /
have array target person of most priority/stick with top prior
Hello Everyone.
I am currently making a script that allows it to detect players and their ships, and then target them according to priority number. (the lower the number, the more important it is).
I have a somewhat functioning one, but it keeps rapidly changing between targets, as well as being unable to focus on the top target. (it targets him for a second before targeting a less important Transform again).
It is at the FindTarget function.
would someone be nice enough to take a look and let me know where I messed up at? Thank you for your time!
public var fieldOfViewAngle : float = 110f;
var sharkMain : scoutshipAI;
private var col : SphereCollider;
private var search : boolean = true;
var targets : Transform[];
private var empty : Transform;
var tarNum : int = 999;
var newTarget : boolean = false;
function Awake()
{
col = GetComponent(SphereCollider);//needed to detect character in field of view
}
function Update ()
{
if (targets.Length == 0)
{
var newContents = new Array(targets);
newContents.Add(empty);
targets=newContents.ToBuiltin(Transform); //Array to unity builtin array
}
if (search == true)
{
findTarget();
}
}
function OnTriggerEnter(other : Collider)
{
if(other.tag == "Player" || other.tag == "ship" ||other.tag == "Bullet") //make sure target is player or other "threat"
{
for(var i : int = 0; i < targets.Length; i++)
{
if (other.transform != targets[i])
{
if (targets[0] == null) // assign target to blank spot;
{
targets[0] = other.transform;
var newContents = new Array(targets);
targets=newContents.ToBuiltin(Transform); //Array to unity builtin array
}
else //add in new spot for new target
{
newContents = new Array(targets);
newContents.Add(other.transform);
targets=newContents.ToBuiltin(Transform); //Array to unity builtin array
}
}
for(var j : int = 0; j < targets.Length-1; j++) //needed to check for extra copies on the target list.
{
//compares two different list spots and see if they are the same. if so, delete the extra.
if (targets[i] == targets[j])
{
if (j != i) ///make sure it's not comparing the same list spot.
{
newContents = new Array(targets);
newContents.RemoveAt(i);
targets=newContents.ToBuiltin(Transform); //Array to unity builtin array
}
}
}
}
}
}
function findTarget()
{
search = false;
for(var i : int = 0; i < targets.Length; i++)
{
//maybe don't need this here. move to J maybe?
if (targets[i] != null) //we have at least one target in the array
{
for(var j : int = 1; j < targets.Length; j++) //needed to check for extra copies on the target list.
{
//chooses between two or more targets with same priority level, and make that the target
if (targets[i] != targets[j])
{
if (j >= i)
{
Debug.Log(tarNum);
//needs more work. keeps cycling through despite priority. (targets player for a second before target other one again);
if (tarNum > (targets[j].GetComponent(priorityScript).priorityNumber))
{
Debug.Log("greater than tarnum");
tarNum = targets[j].GetComponent(priorityScript).priorityNumber;
sharkMain.Player = targets[j];
sharkMain.provoked = true;
yield WaitForSeconds(1);
}
else if (tarNum < (targets[j].GetComponent(priorityScript).priorityNumber))
{
Debug.Log("lesser than tarnum");
tarNum = targets[i].GetComponent(priorityScript).priorityNumber;
sharkMain.Player = targets[i];
sharkMain.provoked = true;
yield WaitForSeconds(1);
}
else if (tarNum == targets[j].GetComponent(priorityScript).priorityNumber)
{
if (newTarget == false)
{
newTarget = true;
choose = Random.Range(0, targets.Length);
Debug.Log("equal to tarnum");
Debug.Log("tarNum range is from " + i + " to " + j);
tarNum = targets[choose].GetComponent(priorityScript).priorityNumber;
sharkMain.Player = targets[choose];
sharkMain.provoked = true;
yield WaitForSeconds(1);
}
}
}
}
}
}
}
yield WaitForSeconds(5);
search = true;
newTarget = false;
}
function OnTriggerExit (other : Collider)
{
if(other.tag == "Player" || other.tag == "ship" ||other.tag == "Bullet") //make sure target is player or other "threat"
{
for(var i : int = 0; i < targets.Length; i++)
{
if (other.transform == targets[i])
{
var newContents = new Array(targets);
newContents.RemoveAt(i);
targets=newContents.ToBuiltin(Transform); //Array to unity builtin array
}
}
}
if (sharkMain.Player != null)
{
if (other.tag == sharkMain.Player.tag)
{
sharkMain.Player = null;
sharkMain.provoked = false;
}
}
}
Comment