using a coroutine to gather resources.

Right now i am trying to achieve allow my builder to mine gold. i’ll post the steps in mind and the issues i am facing along with my code.

1)right click on mine to initiate mining.

2)move from Point A (the base) to Point B (inside the mine) in a loop.

  1. in Step 2 i want to make the game object(my builder) look at point A if he’s at point B and vice versa.

4)once at Point B i want wait a few seconds. increment my gold counter. then continue.

  1. if i rightclick away from the mine the coroutine should stop.

i am stuck on step 3. here’s my code so far.

public float speed;
public Transform ref1;
public Transform ref2;
public GUIText myText;

private Ray ray;
private RaycastHit hit;
private Vector3 pointA;
private Vector3 pointB;
private Vector3 currentPos;

private void Start() {
	pointA = ref1.transform.position;
	pointB = ref2.transform.position;
}

private void Update() {
	ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	currentPos = transform.position;
	StartMining ();
}

IEnumerator PingPong() {
	ReachPointA ();
	while (true) {
		float i = Mathf.PingPong(Time.time*speed,1);
		transform.position  = Vector3.Lerp (pointA,pointB,i);
		yield return new WaitForSeconds(50);
	}
}

IEnumerator Mining() {
do {
StartCoroutine(“PingPong”);
yield return new WaitForEndOfFrame();

	} while(true);
}
private void StartMining() {
	if (Physics.Raycast (ray, out hit) && Input.GetMouseButtonDown(1)) {
		if(hit.transform.gameObject.GetComponent<GoldMineGUI> ()) {
			StartCoroutine("Mining");
		}
	}
}
private void ReachPointA() {
	transform.position = Vector3.Lerp (transform.position, pointA, 0.3f * Time.deltaTime);
}

Use LookAt, see docs for syntax and UA/Google for many examples.