- Home /
moving a platform
Hi guys, I'm still a novice and I need help with this code, there's something I don't understand In this code I'm trying to make a platform move from left to the right in a loop, the problem I have is that when the platform must come back to is original position, instead of moving it jumps to the original place. I don't understand why
function Update () { if(!ontrigger){ if(orizz_vert){ //muovi orizzt transform.position = Vector3.MoveTowards(partenza, arrivo, Time.time * velocita); //arrivato = true; if(Vector3.Distance(transform.position, arrivo) <= 0 && arrivato){ WaitForSeconds(pausa); Debug.Log("entrato"); var niente : Vector3 = partenza; //scambio partenza e arrivo partenza = arrivo; arrivo = niente; arrivato = false; } } else { //muovi vert
}
}
}
Answer by Statement · Apr 03, 2011 at 12:01 PM
One reason your code makes your object jump is because your MoveTowards work on the two variables that are swapped (partenza become arrivo and arrivo become partenza). First of all, this isn't how MoveTowards is meant to be used. It looks like you are sort of lerping. Second, you're using Time.time instead of Time.deltaTime. Time.deltaTime is usually used with MoveTowards, and usually you put the same variable in the first parameter as the return value. (The value being moved toward arrivo).
Your current solution would place transform.position at the new arrivo once it hits the old arrivo.
Why? Because you're using Time.time * velocita, which never reset.
transform.position = Vector3.MoveTowards(transform.position, arrivo,
Time.deltaTime * velocita);
Instead of.
transform.position = Vector3.MoveTowards(partenza, arrivo, Time.time * velocita);
Answer by Jesse Anders · Apr 03, 2011 at 11:15 AM
I'm not sure if this is the cause of the problem, but this conditional:
Vector3.Distance(transform.position, arrivo) <= 0
Will only evaluate to 'true' if transform.position and arrivo happen to be exactly the same, which will be unlikely in the general case due to floating-point error.
Also, distance is by definition positive, so the '<' part of the comparison shouldn't be necessary.
that condition works fine, that Debug.Log show me "entrato" in the scene. I think the problem is in that transform.movetowards, but I don't know what to do
Your answer
