- Home /
Creature AI. Child with collider, parent with navmesh agent
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?
Look, this system you've made isn't exaclty the best way to do that, but I think I know what you want. I haven't done JavaScript for a long time, but here is how you could do it in C#:
Access the script - This means you have to make your 'agent' variable public(, or internal and access the child script):
//SetDestination for parent, from child
private NameOfParentScript ps;
void Start() {
ps = GetComponentInParent<NameOfParentScript>();
}
void OnTriggerEnter(Collider c) {
if(c.gameObject.tag == "Prey")
ps.agent.SetDestination(c.transform.position);
}
Actually pass the argument to your function - In JS I think its something like:
//Parent
function walkTo(target : Vector3) {agent.SetDestination(target);}
//Sense
function OnTriggerEnter(o : Collider) {
GetComponentInParent.<NameOfParentScript>().Send$$anonymous$$essageUpwards("walkTo", o.transform.position);
}
Answer by Vice_Versa · Jun 27, 2015 at 09:30 PM
I think you want this at the top of your script
var preyDestination : Vector3;
then in the trigger function
OnTriggerEnter(other: Collider)
{
preyDestination = other.gameObject.transform.position;
}
then you would grab the preyDestination value in your first script, or send it to it in the second script. I have no idea how referencing other scripts works in javascript to be honest
Your answer
Follow this Question
Related Questions
Why Do Raycasts Ignore NavMesh Agents 4 Answers
Can you bake nav mesh in prefab mode? 0 Answers
Move NavMeshAgent at a constant speed 1 Answer
Navmesh path without agent 1 Answer
How come certain Children don't cast an Overlapsphere? 1 Answer