# How do I create a Collision Field so I know if a certain Object is in that field?

How do I Create a Collision Field So I know if a certain Object is in that field?

Im making very basic AI for guards in a stealth game and I’m having tremendous difficulty creating a script that will work for what I need. My current one is riddled with errors and I can’t figure out how to do this without the field being solid and non-pass-through-able if you know what I’m saying.

There are 2 sides to this script

The Fields Side, which checks if the player is in the field and puts it into a variable called “Colliding”

Script:

``````static var Colliding : boolean = false;

function OnCollisionStay(col : Collision){

if(col.gameObject.tag=="Player")

{ Colliding = true; }

else

{ Colliding = false; }

}
``````

And The Guards Side, which upon knowing the object is in it’s field, will turn and shoot at the player

Script:

``````var Field : GameObject;

var projectile : Rigidbody;

var speed = 20;

var bool : boolean;

var Person: GameObject;

fuction Update()

{

if(FieldScript.Colliding && bool)

{
var clone : Rigidbody;

Transform.LookAt(Person.Transform.position);

Debug.Log("TR");

clone = Instantiate(projectile, transform.position, transform.rotation);

clone.velocity = transform.TransformDirection (Vector3.forward * speed);
}
}
``````

NOTE: The field script does seem to work but only when the cube is touching the side of the collision field but the guards side does not work

This sort of thing is surprisingly tricky. Notes:

Your logic in the Stay function is bad. Suppose a player and a goat are in the area. The player sets it to true, but the goat sets it back to false. Also, when the player leaves an empty area, it stays true. All this does is check “was the last thing in the area, the player?”

One trick is to use Enter/Exit. “Enter: if player, true.” “Exit: if player, false.” No elses (if a goat wanders in and out, it has zero effect on the player status. Ignore the goat.) It’s like watching the doors in a shack. You aren’t watching the guy, but if you see him go in and didn’t see him come out – he must still be in there.

Could also put the check on the player. So would look like: “Enter: if tagged guardZone, set player zone to that number.” “Exit: if tagged guardZone, set player zone to -1.” Assuming guard zones never overlap or touch (you fully leave one before entering another.)

The problem with the Stay method is you really want to say “if Stay fired this frame on the player, true. If Stay did not fire for the player, false.” The way to do that is to set to false at the start of every frame (or end of previous frame.) Then let all the Stays run and maybe set to true. Then run the guard code. But it’s difficult to get that timing, since your code can’t say “OK, now run OnTriggerStay for the player.”

You need to use Triggers.

In the Cllider component of your field, make sure that “Is Trigger” is checked, and in the field side script use:

``````function OnTriggerStay (col : Collider)
{
...
}
``````

Please note that the contents of OnTriggerStay is executed every frame while the “Player” character is in the field. You have 2 more fuctions associated with triggers which are:

``````function OnTriggerEnter (col : Collider)
{
...
}
//--AND--
function OnTriggerExit (col : Collider)
{
...
}
``````

Now, these 2 functions will execute their content when a collider enters or exits the field. Pick and choose the function according to your needs.

ALSO, if you have more than one guard, do NOT use “static” in the field script. If one of them changes they all change.