- Home /
How to move an object from another position using Vector3.moveTowards precisely and slowly by swiping (mobile)
I want to move an object to another position precisely and slowly. I was at first using Vector3.Lerp but I read that it never reaches the point precisely or it slows down as it approaches the target point. Here is my code:
if ((lp.x > fp.x))
{ //If the movement was to the right)//Right swipe
Debug.Log ("Right Swipe");
objMover ();
}
......
void objMover()
{
if (myTransform.position == pos1) {
float step = Time.deltaTime * 2.0f;
myTransform.position = Vector3.MoveTowards(pos1,pos2,step);
}
When I use Lerp, it reaches maybe 1/3 of the way to the target position. When I use move towards, it doesn't move at all. I also tried checking using the Vector3.distance to repeat the moveTowards but fails.
Answer by Garazbolg · Jul 19, 2017 at 01:25 PM
I don't know where you read that Lerp isn't precise but I don't think its the case. But about the slowing down part it just depend on the way you implement it.
Also Lerp and MoveTowards behave the same. They aren't Coroutine though, you need to call them for each movement. All they do is something like :
Vector3 Vector3_Lerp(Vector3 a, Vector3 b, float step){
return a + (b - a) * Mathf.Clamp01(step);
}
Now back to your code :
if (myTransform.position == pos1) {
Will only make you object move once. The next frame when it started moving toward pos2 it won't be on pos1.
I think what you want to do is use Coroutine like this :
if ((lp.x > fp.x))
{ //If the movement was to the right)//Right swipe
Debug.Log("Right Swipe");
StartCoroutine(objMover());
}
IEnumerator objMover(Vector3 pos1, Vector3 pos2, float speed)
{
float step = 0;
while(step < 1){
step += Time.deltaTime * speed;
myTransform.position = Vector3.MoveTowards(pos1, pos2, step);
yield return null;
}
myTransform.position = pos2;
}
And you might want to prevent your player from doing anything until it is over, a simple flag should do the trick.
I hope it helped you. Good luck !
Thank for answering! I am still starting out with Unity, thanks for the help. Although, I still need to study what is coroutine.
I ended up using lerp, but you have enlightened me about "it only runs one frame". I'll try using that code though!
I was just thinking, can I use if (myTransform.position == pos1){ if (myTransform.Distance(pos1,pos2) <= 0){ //working lerp code } }
would it do the trick of continually running the lerp?
No because myTransform.position == pos1
will only be true when your gameObject is EXACTLY at that position, so the next frame it won't be true.
But you can use this type of condition to check if your object has reached its destination. For exemple with a basic waypoint system :
public Transform[] targets;
private int currentIndex = 0;
public float distanceToTarget = 0.5f;
public float speed = 2f;
void Update()
{
if (Vector3.Distance(transform.position, targets[currentIndex].position) < distanceToTarget)
currentIndex = (currentIndex + 1) % targets.Length;
//$$anonymous$$ove
Vector3 from = transform.position;
Vector3 to = targets[currentIndex].position;
transform.position += (to-from).normalized*speed*Time.deltaTime;
}
And don't forget to accept the answer if it helped you =)
Your answer
Follow this Question
Related Questions
3 coroutines to ease in lerp, MoveToward steady pace, and ease out lerp 0 Answers
Move Transform to Target in X seconds 3 Answers
Vector3.MoveTowards and Quaternion.RotateTowards 1 Answer
Vector3.Lerp result in "laggy" movement while running on iOS devices 2 Answers
Smoothly moving a 2D gameobject along a vector path 1 Answer