- Home /
Problem With Vector3.Distance
Alright so I'm working on some AI for a zombie and this should be fairly simple. If the Zombie is within hit range it stops moving towards me and hits me. This accomplished by the attack() function. Problem is that for some reason even if neither of us is moving the distance calculation continues to go up even if we're getting closer. So here is the code and tell me if you can spot what exactly is going wrong here.
function AttackPlayer () {
var lastVisiblePlayerPosition = target.position;
while (true) {
if (CanSeeTarget ()) {
distance = Vector3.Distance(transform.position, target.position);
Debug.Log(distance);
// Target is dead - stop hunting
if (target == null)
return;
// Target is too far away - give up
if (distance > attackRange)
return;
lastVisiblePlayerPosition = target.position;
if (distance >= dontComeCloserRange) {
MoveTowards(lastVisiblePlayerPosition);
}
if (distance < HitRange) {
Attack();
}
}
else {
yield StartCoroutine("SearchPlayer",
lastVisiblePlayerPosition);
// Player not visible anymore - stop attacking
if (!CanSeeTarget ())
return;
}
yield;
}
}
It's worth pointing out that I monitored the actual number given by the Vector3.Distance equation while also monitoring the real position of the target, i.e. the player, and the transform, i.e. the zombie. And again even though both were standing still the distance continued to rise until 30 which is what the variable attackrange is set to. After that it never resets either, the distance just continuously goes up.
distance is not a static var, is it?
you should make sure the Vector3s being used to calculate the distance aren't changing if you're not moving.. judging by this, they may be somehow
add some more debug.logs:
distance = Vector3.Distance(transform.position, target.position);
Debug.Log(distance);
Debug.Log(transform.position);
Debug.Log(target.position);
may help see whats going wrong
I did that before already and even if the 2 vector 3's are the same the number bounces between 2 numbers. I'm thinking about writing my own distance function to see if it works.
if that's true, you must be accessing distance somewhere else I guess.. Vector3.Distance should work fine here.. you didn't answer, is distance static? are there multiple zombies in your scene?
No I initially had distance declared within that function so it would have been the only one using it. I then changed it to a private var that could be seen by any function in the script for debugging purposes. I found a fix for the problem although I'm not sure how it works. Essentially I just removed the script from the model and put it back on and that somehow fixed the problem.
Answer by TheDavil86 · Jul 29, 2012 at 01:06 PM
Ok I figured out the answer to my question. I'm not really sure what the problem was but by deleting the prefab and re-creating it, it fixed the problem. So anyone else that may experience this problem just do the same.
Your answer
Follow this Question
Related Questions
Vector3.Distance not working properly? 3 Answers
Find a position by knowing the directionVector and the distance to this point 1 Answer
Move an object to a specific distance 0 Answers
Change the "y" of a variable 1 Answer
Vector3(x,y,variable)? 0 Answers