Noob Collider Questions

Hello. I’m just getting started with an iPhone app and already hit some roadblocks. I have a enemy bot. I want him to have a view cone in the direction that he is looking; meaning that if you are standing behind him, he will not detect you.

Question 1: What is the most efficient way to do this in Unity? I did not notice a primitive for this purpose.

Question 2: I would not want this cone to be active all of the time. So I would only check the cone when the player has entered the sphere collider of the enemy. How do you turn on/off colliders dynamically for better performance?

Thank you in advance.
-Wil

You could create a Mesh shaped like the cone, and use a trigger on that - but possibly, especially, since you are targeting the iphone, it would be more advisable to write some code to test against it yourself.

Just test the angle between the distance vector, and the bot’s look direction:

if(Vector3.Angle(botTransform.forward, playerTransform.position - botTransform.position) <= coneAngle)
    BotCanSeeYou();

Thank you Jonas for the explanation and the code. I can’t wait to implement it this weekend when I’ll finally have a solid block of time to work on the game. :o

That might be the alternative with the least work involved for you, but a collider is definitly better performance-wise.

This came from Joachim Ante at the Unite 07 conference.