- Home /
Adding Distance in javascript
How can i add distance into a javascript? I'm trying to make an AI script and below is the part which involves distance
//Moving var moveSpeed = 3.0;
//Transform Variables var Target : Transform; var bulletPrefab : Transform; var dead : Transform;
//Attacking var attackRange = 30.0;
function Attack () { var distance = Vector3.Distance(transform.position, Target.position);
if(distance > attackRange) { transform.LookAt(Target); } }
thx man i didnt realize i had greater than and less than mixed up
Answer by · Jan 19, 2011 at 07:57 AM
Your script should work, except I think you might have meant to test for 'less than' instead of 'greater than'.
i.e.
if ( distance <= attackRange )
// attack the enemy, because you're within range
Answer by cjmarsh · Jan 19, 2011 at 07:18 AM
Try separating the variable and the calculation for the distance:
var distance : float;
distance = Vector3.Distance( transform.position, target.position );
//Also, be sure to name your variables in lower camelCase
Your answer
Follow this Question
Related Questions
Too Close To The Trap and melee attacking enemys. 1 Answer
How to make AI enemy do damage and only attack inside a distance or range 2 Answers
Attack Script problem please help 1 Answer
Why is the method being called every frame even though it is InvokeRepeating? 1 Answer
Help implement such attacks 1 Answer