edutainPeople edutainPeople - 2 months ago 19
C# Question

Unity: Moving a sprite to coordinate (tween)

I'm new to Unity C#, and have tried to get a simple sprite to move to i.e. coordinates 3,5 ; 4,5. I cannot make this work.

Any help will be highly appreciated.

I have tried the following:

public class Zookeeper : MonoBehaviour {

public float speed = 50f;
private Vector2 target;

void Start () {
target = transform.position;
}

void Update () {
transform.position = Vector2.MoveTowards(
transform.position, target, speed * Time.deltaTime
);
}
}

Answer

Just change the Start method to something like this:

    void Start () 
    {
        target = new Vector2(3.5f, 4.5f);
    }

You need to operate with target to make it move where you want.