using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyMovement : MonoBehaviour {

	// Use this for initialization
	void Start () {

        int rand = Random.Range(0, 2);

        if(rand == 0)
        {
            this.GetComponent<Rigidbody2D>().velocity = new Vector2(-10f, 0f);
        } else if (rand == 1)
        {
            this.GetComponent<Rigidbody2D>().velocity = new Vector2(10f, 0f);
        }

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

        

	}

     void OnCollisionEnter2D(Collision2D hit)
    {
            

            float dist = this.transform.position.x - GameObject.Find("wall").transform.position.x;
            float dist2 = this.transform.position.x - GameObject.Find("wall2").transform.position.x;
            float dist3 = this.transform.position.y - GameObject.Find("wall3").transform.position.y;
            float dist4 = this.transform.position.y - GameObject.Find("wall4").transform.position.y;

        if (hit.gameObject.name == "wall")
        {

            this.GetComponent<Rigidbody2D>().velocity = new Vector2(10f, dist * 2f);

        }
        if (hit.gameObject.name == "wall2")
        {

            this.GetComponent<Rigidbody2D>().velocity = new Vector2(-10f, dist2 * 2f);

        }
        if (hit.gameObject.name == "wall3")
        {

            this.GetComponent<Rigidbody2D>().velocity = new Vector2(10f, dist3 * 2f);

        }
        if (hit.gameObject.name == "wall4")
        {

            this.GetComponent<Rigidbody2D>().velocity = new Vector2(-10f, dist4 * 2f);

        }
    }
}
