- Home /
The question is answered, right answer was accepted
ai spot you with flashligt on!!!
im trying to make horro game and i have a simple ai script (i like to modified scripts) but i was wondering how can i make my enemy ai spot you when there a spotlight on (flashligt on) near him. do i need to know a special word for the spotlight?
well what i wanted is when the light is on near him he spots me and charge well im gona show you the Ai script that im using.
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
renderer.material.color = Color.yellow;
lookAt();
}
if (Distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if (Distance < attackRange)
{
renderer.material.color = Color.red;
attack ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function attack ()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
well the function look at is the object ( i use a capsul )when near the object its façing you. the function attack juste charge to you. and what i was thinking is to add a new function bellow the attack function. but i have know idea how to make it attack on light on.
Does the spotlight have to be facing him? Does the cone of light have to intersect with his model/face? Or is it just proximity-based?
If it's just by proximity, then just use a combination of Vector3.Distance()
and Light.enabled
to test your light[s].
well what i wanted is when the light is on near him he spots me and charge well im gona show you the Ai script that im using.
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
renderer.material.color = Color.yellow;
lookAt();
}
if (Distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if (Distance < attackRange)
{
renderer.material.color = Color.red;
attack ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function attack ()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
well the function look at is the object ( i use a capsul )when near the object its façing you. the function attack juste charge to you. and what i was thinking is to add a new function bellow the attack function. but i have know idea how to make it attack on light on.
Answer by Nanobrain · Feb 04, 2014 at 07:07 AM
Create a cone shaped mesh, add a mesh collider to it, turn it into a trigger, give it a script and make use of the onTriggerEnter() event, then add it as a child to your enemy character. Now, when your player enters into the cone trigger object onTriggerEnter() will fire. You can then check if the collider parameter is the player object. If it is then set a variable in your enemy object to indicate that the player is within the enemie's light path. Then check that variable in your enemy script.
Follow this Question
Related Questions
How to reproduce a sound on collision? 1 Answer
Need a Basic AI Script for Unity 3.0 2 Answers
Horror Game AI script recommendation? 1 Answer
Need an enemy script that makes him wander and detect 1 Answer
AI Script Problem, Horror Game 0 Answers