using UnityEngine;

public class FollowBox : MonoBehaviour
{
    public Transform target;

    public Rect box;

    private Vector3 _position;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (target == null)
        {
            return;
        }

        _position = transform.position;
        if (_position.x + box.x > target.position.x)
        {
            //Debug.Log($"position {_position}\nBox eval {_position.x + box.x}\nTarget position {target.position}");
            _position.x = target.position.x + box.x + box.width;
        }

        if (_position.x + (box.x + box.width) < target.position.x)
        {
            _position.x = target.position.x - (box.x + box.width);
        }

        if (_position.y + box.y > target.position.y)
        {
            _position.y = target.position.y + box.y + box.height;
        }

        if (_position.y  + (box.y + box.height)  < target.position.y)
        {
            _position.y = target.position.y - (box.y + box.height);
        }

        transform.position = _position;
    }
}