- Home /
Question is off-topic or not relevant
can someone look at this script for me
i need help finding out whats wrong with this. it gives me no error and i can play but once my enemy shows up, it gives me a error. i want him to show up and not move until my flashlights points on him. so im trying to create a raycast to hit the flashlight and when it i want the enemy to chase me, if that makes sense
var Player : Transform;
var moveSpeed = 1;
var maxDist = 5;
var minDist = 1;
var length = 30;
function Update()
{
var hit : RaycastHit;
var ray : Ray = Transform.Position(Vector3.forward);
transform.LookAt(Player);
if(Physics.Raycast(ray, hit, length))
{
if(hit.collider.tag == "FlashLight")
{
if(Vector3.Distance(transform.position, Player.position) >= minDist)
{
transform.position += transform.forward*moveSpeed*Time.deltaTime;
}
}
}
}
Answer by robertbu · Sep 24, 2013 at 01:13 AM
Issues like this one are a good reason why you should always use '#pragma strict' at the top of your files. With the pragma you would have received a compile-time error and would have seen the problem. I suggest you change to a different form of Raycast. Try this:
var Player : Transform;
var moveSpeed = 1;
var maxDist = 5;
var minDist = 1;
var length = 30;
function Update()
{
var hit : RaycastHit;
transform.LookAt(Player);
if(Physics.Raycast(transform.position, transform.forward, hit, length))
{
if(hit.collider.tag == "FlashLight")
{
if(Vector3.Distance(transform.position, Player.position) >= minDist)
{
transform.position += transform.forward*moveSpeed*Time.deltaTime;
}
}
}
}
that works perfect, thanks....i still dont know what was wrong with $$anonymous$$e tho...i fail at at creating scripts :(
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Enemy Health With Raycast Not Working 1 Answer
Enemy raycast chase player 0 Answers
How to Stop Enemy "Shooting Through A Wall" 1 Answer
Follow Nearest Target 1 Answer