make object move towards point unity code example

Example 1: how to make an object point towards the mouse in unity

//Inside your update loop, add the following code:

//First, get the mouse position
Vector2 mousePosition = new Vector2 ( 
  Input.GetAxis("MouseX"), 
  Input.GetAxis("MouseY")
);
//Then, use unity's built-in lookAt function to do all the math for you:
transform.LookAt(mousePosition);

Example 2: move towards target unity

/// <summary>
/// Move 2D sprite towards target
/// </summary>
/// <param name="target"></param>
/// <param name="movementSpeed"></param>
private void Move(Vector3 target, float movementSpeed)
{
    //Move
    transform.position += (target - transform.position).normalized * movementSpeed * Time.deltaTime;
}

Example 3: movetowards unity

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

Example 4: 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 5: how to make an object move towards another in unity

transform.position = Vector3.MoveTowards(transform.position, taretPos, Qiaternion.identiy)