- Home /
Update function don't update
function Update () {
var i:int = 0;
var distanceTargetGuard = Vector3.Distance(transform.position,targetTransform.position);
if(distanceTargetGuard<targetMinDistance){
i++;
targetTransform.position = targetDesiredPosition[i] ;
distanceTargetGuard= Vector3.Distance(transform.position,targetDesiredPosition[i]);
}
MoveAndRotate(targetTransform.position);
}
Hi everyone,
I need somehelp with a problem. So I'm planing to make an object moving towards a target"targetTransform". Whenever this object distance towards the target is smaller than 1, the new target position is set.
here's my code. What happen now is the target only changed once and the object is stuck since the target is not changing.
Anyone have any idea why it doesn't work as I want?
Answer by OperationDogBird · Aug 14, 2012 at 07:51 PM
Since you declare i in the update function i will either be 0 or 1, but never any higher. Even when it is 1, it will only be 1 for a single frame (actually while the condition is met). Declare i outside of the Update function and it will work. make sure there is a limit on the index as well, such as
if(i>=length)i=0;
thanks...sorry if my question is too stupid...I'm new in these. :D
Your answer
Follow this Question
Related Questions
Loop to populate array crashes Unity 1 Answer
list.contains isn`t working 1 Answer
Why does this code hang the system?? 2 Answers
Rearranging pre-existing objcets into a grid 2 Answers
Stuck in Teleporter Loop 2 Answers