Move multiple gameobjects

Hi,

I’m trying to move multiple gameobjects towards a specific target position by using a single script. I don’t want to attach my MoveTowards script to every gameobject.

MoveTowards script:

using UnityEngine;

public class MoveTowards : MonoBehaviour
{
    void Update()
    {
        if(VariablesReference.allowPlanetMovement == false)
        {
            Transform[] transform_planets = VariableController.Instance.transform_planets;
            for (int i = 0; i < transform_planets.Length; i++)
            {
                float moveSpeed = VariablesReference.planetMovement * Time.deltaTime;
                Vector2 targetPosition = new Vector2(0, 0);
                transform_planets*.transform.localPosition = Vector2.MoveTowards(transform.localPosition, targetPosition, moveSpeed);*

}
}
}
}
The problem here is that the all transform_planets snap to the targetPosition. It does not move to the targetPosition over time.

The problem is that your first parameter to the MoveTowards() method should be the position of your planet that you are moving, then re-assigning to the new position. Fixing this line will probably do what you expect:

transform_planets<em>.transform.localPosition = Vector2.MoveTowards(transform_planets*.transform.localPosition, targetPosition, moveSpeed);*</em>

You are probably going to have to set a constant that represents how many steps they move towards the target in a “step”. This will allow you to specify how long you want it to take, and how smooth the travel is.