- Home /
Is the Player Being Looked at
Hello, I am new to unity and not very experienced. Currently I've been trying to make a horror game and I want to make it so that the enemy does and action (attacks) if you look at it (in your screen view). Please give advice if you know how to do thins in C#.
THANKS!!! :)
what type of view is it? if it is first person view you can use something like
//On Enemy
if(renderer.isVisible)//Do something
there are many ways to do it, this is probly the quickest to write and understand. The isVisable will be true if the enemy is being rendered by ANY camera in the scene(this includes the scene camera if you dont have $$anonymous$$aximize On Play enabled and are viewing the enemy), so if there are more than one camera the results will most likely not be what you want
if it's first person (as it seems to be by your description) the above suggestion is easiest..
also note that even the objects shadow being visible is enough to return true here..
http://docs.unity3d.com/Documentation/ScriptReference/Renderer-isVisible.html
and also keep in $$anonymous$$d this works even in the editor, not just in playmode
OperationDogBird, I tried the example you give and it seems promising, however, it say that my 3d model does not have a "renderer" attached to it, how do I add one?
And yes, it is first person. Thank you for the advice.
Sorry for the spelling error, good catch Seth. The model should have a renderer attached bcz otherwise you cant see it. click the prefab in the scene and view its components in the inspector, there should be a component called $$anonymous$$esh Renderer. If not(which is hard to believe) you can add from the top bar of unity- Component/$$anonymous$$esh/$$anonymous$$esh Renderer.
I did think of this, tho a better solution is to just have all the meshes parented to the main mesh and not an empty object. also this portion
var the$$anonymous$$odel[] : Renderer;
wont work, you need to exclude the [] part of the declaration(unless theres some secret C# thing i dont know about). If you wanted to get components, the correct type would be
var childrenR : Renderer[];
Then you can access the individual ones by using indexing.
Answer by cupsster · Aug 02, 2012 at 09:35 AM
More propper solution would be to test enemy bounding box center position against view cone. Some vector math library was posted on wiki which does this calculus.
Your answer
Follow this Question
Related Questions
Need an enemy script that makes him wander and detect 1 Answer
Need help with my enemy spawn script. 1 Answer
free roaming enemy ai 1 Answer
Make enemy appear in front of player and then disappear at random times 2 Answers
Enemy AI problems 2 Answers