void Update () { GameObject[] objects; object = GameObject.FindGameObjectsWithTag("sometag"); transform.position = Vector3.MoveTowards(transform.position, objects[0].transform.position, 2f * Time.deltaTime); } 

You need to add a random (in fixed limits) offset of the end point along the x, y axes. How can this be done?

  • docs.unity3d.com/en/current/ScriptReference/Random.Range.html think what you need to generate a random number within - Alex Shimansky
  • When do you need to shift? In the process of movement (like, a moving object staggers), or should the destination be displaced (scatter when firing, for example)? And for God's sake, rename the array, at least in objects :) - eastwing
  • The destination must be shifted, because the script will be attached to several objects and will not be comme il faut if all of them at the end move to the same point. Now I will try to do the example from the documentation - FullyRetarded

1 answer 1

The result was something like this:

 public class test : MonoBehaviour { public GameObject somegameobject; float offset_x, offset_y; void Start () { randomizer(); } void randomizer() { offset_x = Random.Range(5f, 17f); offset_y = Random.Range(5f, 7f); } void Update () { GameObject[] gameobjects; gameobjects = GameObject.FindGameObjectsWithTag("Sometag"); Vector3 offset = new Vector3(gameobjects[0].transform.position.x + offset_x, gameobjects[0].transform.position.y + offset_y); transform.position = Vector3.MoveTowards(transform.position, offset, 2.5f * Time.deltaTime); } }