GameObject moving left to right AND up

Hi!
I’ve got a rather simple question (I guess), but can’t get it to work. I’ve got a GameObject which moves left to right on a Loop. It’s an instatiated Object. I found the script here at UnityAnswers:

public class ghostMovement : MonoBehaviour {

	public float delta = 0.5f;  // Amount to move left and right from the start point
	public float speed = 2.0f; 
	private Vector2 startPos;



	void Start () {
		startPos = transform.position;

		    		Invoke ("GhostDeath", 1f);


	}
	
	 void Update () {
	 Vector2 v = startPos;
		v.x += delta * Mathf.Sin (Time.time * speed);

		
		transform.position = v;
		
		    			
			     }
		


	void GhostDeath(){
		Destroy (gameObject);
	}
}

This works great. But I can’t get this object to move up at the same time. Adding a negative gravity in the rigidbody2D doesn’t work. Adding something like
v.y += Time.time * 0.5f; doesn’t work either, the sprite doesn’t start to go up at the exact Location and the object starts to appear way too far away from where it should as time goes on.

Cand someone give me an advice on how to solve this Problem?
Thanks in advance!

The second ghost appears higher, because the time goes on. At the beginning, the time is zero, so your ghost moves up from y=0. But if you spawn another ghost later, the time is no more zero. It’s something positive, like 20 (in seconds).

You can save time of the animation start when you spawn a ghost. And as time use difference between current time starting time.

private float startTime;

void Start () {
     startPos = transform.position;
     startTime = Time.time;
}
 
void Update () {
    Vector2 currentPos = startPos;
    float currentTime = Time.time - startTime;

    currentPos.x += delta * Mathf.Sin (currentTime * speed);
    currentPos.y += delta * currentTime * 0.5f; 
     
    transform.position = currentPos;
}