- Home /
Error in distance coding
Hello, I was wondering why my MinDis and MaxDis are not working in the following code
var MoveSpeed : float = 5;
var Player : Transform;
var MaxDis = 10;
var MinDis = 2;
function Start () {
}
function Update ()
{
transform.LookAt(Player);
if(Vector3.Distance(transform.position, Player.position) >= MinDis);
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position, Player.position) <= MaxDis);
gameObject.GetComponent(AIGun).Fire();
}
It doesn't work. The $$anonymous$$imum and maximum distance.
well, if you're under the max, you should always be firing, and if you're over the $$anonymous$$, you're always moving.. so you're saying it doesn't move or fire?
or it doesn't stop moving when it gets within 2?
doesn't stop firing when it gets further than 10?
It fires and moves, but it doesn't stop or start at my max and $$anonymous$$ units
so it runs right into the player? try exposing the distances as vars in the inspector, see what's happening.. Add another var at the top:
var currentDistance : float;
then inside Update(), add the line:
currentDistance = Vector3.Distance(transform.position, Player.position);
see if it's working
maybe you just need to adjust the values
Answer by aldonaletto · Jul 28, 2012 at 02:59 AM
There are syntax errors in both **if*s: the semicolon after the if* make it do nothing, and the next instruction is always executed. The code should be like this:
if(Vector3.Distance(transform.position, Player.position) >= MinDis){ transform.position += transform.forward*MoveSpeed*Time.deltaTime; } if(Vector3.Distance(transform.position, Player.position) <= MaxDis){ gameObject.GetComponent(AIGun).Fire(); }
Ok, it works fine. Only problem now is that it doesn't come towards me. It keeps going forward in its regular path.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
BCE0044: expecting }, found 'private'. PLEASE HELP!!!!! 2 Answers
"Expecting ), found ';'" and "';' expected. Insert a semicolon at the end" 1 Answer
Need Help Understanding Script! (Javascript) 1 Answer
Insert semicolon at the end when theres already one? 0 Answers