I have 1 prefab and create 17 pieces on the stage. The coordinates of each prefab are different. But how can I make one animation so that when I click on an object it moves to a given point. Point one for all prefabs.

  • What object you click on is not quite clear and what do you mean by animation - simple movement to a point? ..... Add prefabs to a list or an array, and then in update you will simply loop through this list / array by changing its coordinates - Alexey Shimansky
  • @ Alexey Shimansky. yes, simple moving. But if through a cycle, then for everyone you need to ask your move? !! - Dmitriy Dev
  • one
    well, yes .... you will have something like foreach (var obj : myObjects) { obj.transform.position = Vector3.MoveTowards(obj.transform.position, target.position, step * Time.deltaTime); } foreach (var obj : myObjects) { obj.transform.position = Vector3.MoveTowards(obj.transform.position, target.position, step * Time.deltaTime); } that is, each object will change its position according to the condition - Alexey Shimansky
  • @ Alexey Shimansky. Thank! Only I wrote the skipt for the prefab and there was no need to create an array ... - Dmitriy Dev
  • Well, OK. let's say there is a Move() method for a script hung on a prifab. Anyway, most likely, in the initializer script (which makes it clear that objects need to start moving) will need to hold an array with references to objects. So at the right time just go through the array and say obj.Move() - Alexey Shimansky

1 answer 1

Decision:

 float step = speed * Time.deltaTime; transform.position = Vector3.MoveTowards(transform.position, target.position, step);