- Home /
Enemy stop moving when stared at
I started unity, and scripting a month ago and I am trying to make a monster where if you look at him, he doesn't move (similar to SCP Containment breach, and Slender). I currently have a block childed to the camera, and want it so when the collider hits the monsters collider, he freezes. Can anyone start me off on a script? Thanks in advance
I thought there was a similar question to this a while back, and after searching it was yours. http://answers.unity3d.com/questions/273809/need-an-enemy-script-that-makes-him-wander-and-det.html
I find this interesting, as the concept re$$anonymous$$ds me of the statues in Dr Who =]
It sounds like you have thought about it and made start. The idea is ok, having a invisible block that basically lives in the player view. You are soo close, all you need to do is add an OnTriggerEnter or OnCollisionEnter on the monster script to say freeze someone's looking, then OnTriggerExit/OnCollisionExit to say ok no-one's looking so move.
watch this video : http://www.unity3dstudent.com/2010/07/beginner-b13-trigger-collision-detection/
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html
http://answers.unity3d.com/questions/240136/sliding-door-animation-question.html
EDIT : if you are just starting out, do all the $$anonymous$$i-projects on unity3dstudent (they are about 5 $$anonymous$$utes each) : http://www.unity3dstudent.com/category/modules/beginner/
it's in reverse order, so scroll down and start at Beginner B00 – Adding $$anonymous$$ass / Gravity with Rigidbodies
I checked out the video, and other links. What would I need to add to make it disable a script temporarily once the trigger cube is over the other collider?
You want to disable the movement, yes, but just makes things more complicated for what you want to do (even though disabling the script is possible). Ins$$anonymous$$d, have a boolean on the monster script and check it before the movement, and use OnTriggerEnter and Exit to toggle this boolean. e.g.
var isSeen : boolean = false;
function Update()
{
if (!isSeen) // ! means if (isSeen == false)
{
$$anonymous$$ove$$anonymous$$onster();
}
}
function OnTriggerEnter( other : Collider )
{
if (other.tag == "Player")
{
isSeen = true;
}
}
function OnTriggerExit( other : Collider )
{
if (other.tag == "Player")
{
isSeen = false;
}
}
Is "$$anonymous$$ove$$anonymous$$onster" the script that makes him follow you?
Answer by venhip · Jul 27, 2012 at 02:07 AM
Well, to see if it is within view of any camera, you can check renderer.isVisible:
if(renderer.isVisible) //Checks to see if is viewed by a camera
if you mean, is it in line of sight of the character, that's a bit more complicated though, you would probably use a raycast forward from the character object... Beware: This includes if it is viewed by the editor camera, so while simulating your scene, if your enemy runs where your editor camera is looking, it will output if(renderer.isVisible) as true, so you'd have to be careful to make sure during testing that the camera is looking at the sky or something.
Your answer
Follow this Question
Related Questions
MouseLook restrictions not working 1 Answer
How to reproduce a sound on collision? 1 Answer
Flashlight pickup, battery etc 3 Answers
Killable NPC who can kill player 1 Answer