- Home /
How to stop transform movement at a certain point?
I have this, but is continues forever.
if(condition == true){
transform.localPosition = Vector3.Lerp(transform.localPosition, transform.localPosition + offsetPosition, moveSpeed* Time.time);
}
Answer by hoy_smallfry · Feb 15, 2013 at 07:03 PM
You keep using the "transform.localPosition" in your function, but every time that function gets called, the value of "transform.localPosition" is different because you modified it in the previous call.
There is never a stopping point because your stopping point is "transform.localPosition + offsetPosition", which is always changing. Exactly like a carrot dangling in front of a donkey. The donkey will never get to the carrot because it is always his position + an offset, so no matter where he goes, he will never get to it:
You need to save the start and stop position some place else, that way they will never change when "transform.localPosition" changes. Try doing something like this:
if(condition == true)
{
transform.localPosition = Vector3.Lerp(start, stop, moveSpeed* Time.time);
}
else
{
start = transform.localPosition;
stop = transform.localPosition + offsetPosition;
}
getting error: BCE0017: The best overload for the method 'UnityEngine.Vector3.Lerp(UnityEngine.Vector3, UnityEngine.Vector3, float)' is not compatible with the argument list '(float, float, float)'.
This was using .x on the start and stop. With those gone, says UnityEngine.Vector3.Lerp(UnityEngine.Vector3, UnityEngine.Vector3, float)' is not compatible with the argument list '(float, float, float)'.
That error is saying that the function 'Vector3.Lerp' pnly accepts '(Vector3, Vector3, float)' as its parameters, but you are givng it '(float, float, float)'