I am making a simulation of an environment with creatures roaming about. There are predators and prey. When a predator senses a prey it chases it.
I have acheved this using navmesh, OnTriggerEnter and agent.SetDestination.
Now i want the predator to eat the prey. My idea is to have muliple children with a collider attached, detecting the distance between the two. Like having a ‘sense’ collider and a ‘bite’ collider.
My problem is i dont know how to send the ‘other’ variable from the childs OnTriggerEnter to my parents SetDestination.
This is my code so far
Parents javascript:
#pragma strict
private var agent: NavMeshAgent;
var eaten : int;
function Start () {
agent = GetComponent.<NavMeshAgent>();
Invoke("walk", 0.5);
}
function Update () {
}
//InvokeRepeating("walk", 0, 5);
function walk () {
var randomTime = Random.Range(0, 5);
var randomDirection = transform.position + Random.insideUnitSphere * 20;
agent.SetDestination(randomDirection);
Invoke("walk", randomTime);
}
function walkTo() {
agent.SetDestination(AIname.gameObject.transform.position);
}
and the childs ‘sense’ javascript :
#pragma strict
function Start () {
}
function Update () {
}
function OnTriggerEnter(other : Collider) {
var AIname : String;
//???
AIname = other.gameObject.name;
SendMessageUpwards ("walkTo", AIname);
print ("hit");
}
Any help would be appreciated. Is there a better method?