using UnityEngine;
using System.Collections;

public class Spider : MonoBehaviour {

	public int speed=2;
	public int direction;

	public float originalYPosition;
	public float newPosition;
	public int  webLength;
	public Rigidbody2D body;
	public bool idle;
	public float timePassed;
	public float timeIdle;

	// Use this for initialization
	void Start () {
		body = this.gameObject.GetComponent<Rigidbody2D> ();
		originalYPosition = this.transform.position.y; 
		newPosition = originalYPosition - webLength;
			}

	// Update is called once per frame
	void Update () {


			SpiderRoam ();

	}

	void SpiderRoam()
	{
		this.transform.Translate (Vector2.up * direction * Time.deltaTime * speed);
		if (direction > 0 && gameObject.transform.position.y >= originalYPosition) {
			direction = 0;
		} else if ((direction < 0 && gameObject.transform.position.y <= newPosition)) {
		
			direction = 0;
		}
		if (direction == 0) {
			timePassed += Time.deltaTime;
		}
		if (timePassed > timeIdle) {
			if (transform.position.y >= originalYPosition) {
				direction = -1;
			} else
				direction = 1;
			timePassed = 0;
		}
	}



}
