make an object move towards another unity code example

Example 1: movetowards unity

//put this in update method and disable rigidbody2d (gravity)
void Update()
{
transform.position += (target - transform.position).normalized * movementSpeed * Time.deltaTime;
}

Example 2: how to move towards an object unity

//This will work for 2d or 3d platforms
//Make sure to call in Update or else it wont work
Vector3.MoveTowards(transform.position, taretPos, Qiaternion.identiy)

Example 3: unity how to move a gameobject towards another gameobject

// the script has been edited a little but is still very similar to the original post
	public float speed;
	public GameObject object1; // The game object that moves.
	public GameObject object2; // the game object that Object 1 moves to.

	void FixedUpdate()
	{
		// Calculate direction vector.
		Vector3 dirction = object1.transform.position - object2.transform.position;

		// Normalize resultant vector to unit Vector.
		dirction = -dirction.normalized;

		// Move in the direction of the direction vector every frame.
		object1.transform.position += dirction * Time.deltaTime * speed;
	}

Tags:

Misc Example