using UnityEngine;
using System.Collections;

public class VictimChaser : MonoBehaviour {
    public float speed = 1.0f;
    private VictimFinder victimFinder;
    private bool isGrounded = true;
    private Rigidbody body;

    void Start() {
        victimFinder = GetComponent<VictimFinder>();
        body = GetComponent<Rigidbody>();
    }

    void Update() {
        if (victimFinder.victim != null) {
            body.velocity = new Vector3((victimFinder.victim.transform.position.x > transform.position.x) ? speed : -speed, 0, 0);

            // ensure character cannot get stuck (collider issues even with 0 friction)
            if (isGrounded) {
                transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y + 0.001f, transform.localPosition.z);
            }
        }
    }


    public void OnCollisionEnter(Collision collision) {
        if (collision.gameObject.tag == "Ground") {
            isGrounded = true;
        }
    }
}
