- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
bleach2427 · Apr 25, 2014 at 08:12 PM ·
aishooting
I am trying to get my enemy to follow and shoot at my fps i have a script but i am geting errors can someone help me please
var distance;
var target : Transform;
var lookAtDistance = 15.0;
var attackRange = 10.0;
var moveSpeed = 5.0;
var damping = 6.0;
var Bullet : Transform;
var Spawn : Transform;
private var isItAttacking = false;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
renderer.material.color = Color.yellow;
lookAt ();
}
if(distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if(distance < attackRange)
{
attack ();
}
if(isItAttacking)
{
renderer.material.color = Color.red;
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function attack ()
{
is ItAttacking = true;
var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation);
pel.rigidbody.AddForce(transform.forward * 8000);
}
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}
}
}
Comment
Answer by perchik · Apr 25, 2014 at 08:11 PM
You might want to think of indenting your code, so that it's more readable (like this, here's part of your update:
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
renderer.material.color = Color.yellow;
lookAt ();
}
if(distance > lookAtDistance)
{
//etc
Then, after you do that, you might notice a lot of extra } in your code. Also, what errors are you actually getting? Right away I see that on line 45 you have a space in your variable name, and it looks like line 49 is outside of any function. Pretty sure you have three extra } at the end too.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Controlling AI Movement 0 Answers
enemy folow whats wrong with it 0 Answers