S
S
Shallmick2021-09-10 22:44:16
Unity
Shallmick, 2021-09-10 22:44:16

How to move an object from point to point?

Here is my code how to make an object move back and forth indefinitely

public class MoveEnem : MonoBehaviour
{
    public Vector2 startPos;
    public Vector2 endPos;
    public float step;
    private float progress;
    void Start()
    {
        transform.position = startPos;
    }

   
    void Update()
    {
        transform.position = Vector2.Lerp(startPos, endPos, progress);
        progress += step;
        
        
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2021-09-10
@Alexander_020

You need to loop progress. For example, you can do it like this:

transform.position = Vector2.Lerp(startPos, endPos, 0.5f+0.5f*Mathf.Sin(progress));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question