How to move a GameObject with an animation

I’d like to move my GameObject upstairs with an animation (just a moving animation, like it’s smoothly moving up). However, I can’t seem to figure out how this works. I’ve tried several methods, all of them are ‘teleporting’ (re-positioning) the GameObject. Here is my current code:

Some code may sound like bullshit, but it was all for testing purposes only

Vector2 startPosition = new Vector2(pMenu.transform.position.x, pMenu.transform.position.y);

Vector2 endPosition = new Vector2(pMenu.transform.position.x, 0.03f);

pMenu.rigidbody2D.velocity = new Vector2(pMenu.rigidbody2D.velocity.x, 0);

pMenu.transform.position = new Vector2(pMenu.transform.position.x, 0.03f);

pMenu.transform.position = Vector2.Lerp(startPosition, endPosition, 5000);

pMenu.transform.Translate(new Vector2(pMenu.rigidbody2D.velocity.x, 10));

pMenu.transform.Translate(new Vector2(0, 500) * Time.deltaTime);

How can I move the gameobject up, so it’s like… sliding up to the desired Y coordinate?

For the benefit of others who might stumble across this later, here is the solution that I came up with.

Create a new behavior/controller script to handle smooth motion and attach it to any/all objects that you want to translate smoothly around the scene. You can use the script I provide below. Once that is attached to the GameObject, simply call SetDestination on the object’s GlideController and it will smoothly move to the specified location.

using UnityEngine;

public class GlideController : MonoBehaviour {
	public float speed;

	private Vector3 destination;

	void Start () {
		// Set the destination to be the object's position so it will not start off moving
		SetDestination (gameObject.transform.position);
	}
	
	void Update () {
		// If the object is not at the target destination
		if (destination != gameObject.transform.position) {
			// Move towards the destination each frame until the object reaches it
			IncrementPosition ();
		}
	}

	void IncrementPosition ()
	{
		// Calculate the next position
		float delta = speed * Time.deltaTime;
		Vector3 currentPosition = gameObject.transform.position;
		Vector3 nextPosition = Vector3.MoveTowards (currentPosition, destination, delta);

		// Move the object to the next position
		gameObject.transform.position = nextPosition;
	}

	// Set the destination to cause the object to smoothly glide to the specified location
	public void SetDestination (Vector3 value) {
		destination = value;
	}
}

Animation usually refers to Unity’s Animation system. What you are trying to do is move something over time either by transform or by Rigidbody2D. Here are a couple of lines of code to get you started. Note that each one needs to be called repeatedly in Update().

pMenu.transform.position = Vector3.Lerp(pMenu.transform.position, endPosition, speed * Time.deltaTime); 

or:

pMenu.transform.position = Vector3.MoveTowards(pMenu.transform.position, endPosition, speed * Time.deltaTime);    

You might also take a look at iTween or the MoveObject script in the Wiki.