- Home /
Making an Enemy stop when visible to Player...
I have a script to make my enemy simply follow the player
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
var target : Transform; //the enemy's target
var moveSpeed = 5;
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
However i've spent ages trying to work out how i can make it so that when the enemy is visible to the player the enemy stops moving. I've looked into raycasting etc but can't seem to get it right. If someone could write a script that somehow changes the move speed variable to 0 when the enemy is visible to the player i would greatly appreciate it , Thanks =D
Answer by robhuhn · Aug 08, 2013 at 10:44 AM
If the player can actually see with a camera attached you could use renderer.isVisible or OnBecameVisible
If the enemy is rendered this would mean he's visible? Wouldn't that mean he would always be moving
renderer.isVisible is true if the enemy is rendered by any camera (within the cameras frustum). So if I get you right you could attach a script to the enemy:
function OnBecameVisible() {
Freeze();
}
function OnBecameInvisible() {
$$anonymous$$ove();
}
And keep in $$anonymous$$d that the scene view cam also forces to call these methods:
Note that object is considered visible when it needs to be rendered in the scene. It might not be actually visible by any camera, but still need to be rendered for shadows for example. Also, when running in the editor, the scene view cameras will also cause this function to be called.
Thanks for the script however it comes up with
Assets/Thestopscript.js(4,20): BCE0043: Unexpected token: :.
There's something wrong with the colon? obviously i'm new to coding in unity ;)
I FINALLY F*C$$anonymous$$*G DID IT 2 DAYS OF WOR$$anonymous$$ING ON THIS
Answer by creighcl · Aug 07, 2013 at 06:04 PM
You're on the right track. You'll want to use a raycast that zips from the badguy's position to the player's position and see what is returned by the raycast. If its the player, then there's nothing in between to obstruct visibility.
Link below to show you how this is done.
You'll also want to find ways to only do these raycast checks only when the two are within a certain a reasonable distance first. Otherwise enemies will always stand still if there's nothing standing between themselves and the player.
Note: Raycast is a method to detect COLLIDERS, not gameobjects. Therefore, this is only going to work if you're working with colliders on your characters.
Related Post : http://answers.unity3d.com/questions/15636/how-can-i-check-the-line-of-sight-to-see-if-the-pl.html
Thanks for the reply i'll see how I get on with this
Answer by Eclipsed · Aug 08, 2013 at 11:05 AM
Just raycast from the player to the enemy, if the raycast is successful then calculate the angle between the forward of the player and the ray you used for the raycast.
If this angle falls between a certain threshold (something like +-45 degrees) then the player can see the enemy.
To calculate the angle use Mathf.Atan2(), check on wikipedia how this function works ;)
Cheers.
Your answer
Follow this Question
Related Questions
AI enemy Stuck at corners 2 Answers
Invisible objects renderer disable? How to stop this? 0 Answers
animation on multiple objects 1 Answer
How to see if a transform is visible without a camera 1 Answer