Would this work to make a FOV for an AI?

If i make a basic square, stretch it out to match a realistic FOV. Then set it to, is trigger, and say this, would it work as a field of view, shooting the projectile on sight of the player?

function Update() {
function OnTriggerStay (other : Collider) {
if (gameObject.tag = “Player”){
var clone : Missile;
clone = Instantiate(projectile, transform.position, transform.rotation);
animation.Play(“alert”, PlayMode.StopAll);
//rigidbody.AddForce(Vector3.forward * speed);
//another question about this, how do I call on a different rigidbody object to shoot forward instead of the box?

clone.timeoutDestructor = 5;
}
}
}

Did you try it? I can tell you right now it won’t work because you have function OnTriggerStay wrapped in function Update.

Also - please format your code with code tags.

sorry, and yes I did but i didn’t work. I should just separate the two functions like this?

also how do you correctly put code tags?

bbcode tag reference:

find code tags in there

A few things.
Update() is a function
OnTriggerStay is a function.

They can not cohabitate. Right now, you have one living in the other. Not to mention, it’s living in Update which is called every few milliseconds.

What you need to do is separate them. Also, your trigger function asks if gameObject.tag. It should be other.tag.

i dont understand, how exactly do I reference the instantiate ridgidbody for a different object in the scene, and I thought I took out the update function.

What if an enemy is inside the FOV but behind a wall?

It seems easier to compare the players forward vector against a vector pointing to the enemy player and use a something like Vector3.Angle(transform.forward, enemy.transform.position - transform.position).
If (angle > FOV / 2) then fire a ray at the enemy player position/direction. If it hits then the enemy is seen otherwise its invisible.

Thats how I do it anyways.

Don’t even bother with that - http://forum.unity3d.com/threads/143875-Using-code-tags-properly

but what about my main question…

you need to learn basic coding before anything else.

i would say, create a trigger. add a trigger function to it. go from there.

well I read unity essentials and I think I have some javascript experience, but where can I go to learn after that?

Like I say. Get a trigger working first. Understand how it works. And you can go from there. There is always a pattern. That is how I approached learning how to code.

If player is in trigger, do raycast? Simple right?
Alternatively we can scale the trigger based on a raycast shot from npc but that’s a bit more advanced.