Moving a particular sprite up and down on an axis regardless of rotation

I am making a spike which pops a needle every t interval. I am able to make this to work. But when I try to rotate my game object, say, sideways leftwards, the popping of needle literally goes up and down not sideways as I expect. I need it to move up and down on an axis. Currently, in my game the game object is facing the green line (y-axis). I want my needle to go up and down on this axis.

Here is what I currently have:

    void Pop(float elapsed)
    {
        float step = elapsed * speed;
        transform.position = Vector3.MoveTowards (transform.position, popped, step);
        transform.
    }

    void Unpop(float elapsed)
    {
        float step = elapsed * speed;
        transform.position = Vector3.MoveTowards(transform.position, relax, step);
    }

Here is how I initialize the target positions:

    void Start ()
    {
        relax  = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
        popped = new Vector3 (transform.position.x, transform.position.y + 0.8f, transform.position.z);

        StartCoroutine (PopNeedles ());
    }

What I am expecting is that the needle to pop out leftwards, since I made it face leftwards (via rotation)! I can circumvent this by making an object and appropriate scripts but that is very stupid and rubbish.

I want it all done in a single game object.

Thanks!

I think the problem is that you set your vectors relaxed and poped only once during start method. When you rotate your object update those vectors too.

I believe I don’t have to? I want it to move to a specific axis. To its local axes. I was able to make this to work by using
this method transform.Translate. But it goes on and on without stopping.

all of your code is working on global position/axis. Probably want to look at localPosition etc.

Thanks, actually I gave up, it doesn’t fit quite well into the game so I decided to ditch the idea. Thanks you very much! I look into it when I come back to that idea again. Maybe I am able to make this work next time.

As I said just recalculate relax and popped when rotating.
Firstly you can simplify those lines:
relax = transform.localPosition;
popped = relax + transform.up * 0.8f;
And make sure they get updated, so put them in to your PopNeedles() coroutine.
It works

using UnityEngine;
using System.Collections;

public class NeedleExample : MonoBehaviour
{
    float speed = 2f;
    float waitTime = 0.5f;
    Vector3 relax;
    Vector3 popped;

    void Start()
    {
        StartCoroutine(PopNeedles());
    }
    void Pop()
    {
        transform.position = Vector3.MoveTowards(transform.position, popped, Time.deltaTime * speed);
    }
    void Unpop()
    {
        transform.position = Vector3.MoveTowards(transform.position, relax, Time.deltaTime * speed);
    }
    IEnumerator PopNeedles()
    {
        while(true)
        {
            relax = transform.localPosition;
            popped = relax + transform.up * 0.8f;
            Pop();
            yield return new WaitForSeconds(0.5f);
            Unpop();
            yield return new WaitForSeconds(0.5f);
        }
    }
}