using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class Unit : MonoBehaviour

{

    public bool selected = false;

    public float floorOffset = 1;

    public float speed = 10;

    public float stopDistanceOffset = 0.5f;


    private Vector3 moveToDest = Vector3.zero;


    private void Update()

    {

        if (GetComponent().isVisible && Input.GetMouseButton(0))

        {

            Vector3 camPos = Camera.main.WorldToScreenPoint(transform.position);

            camPos.y = CameraOperator.InvertMouseY(camPos.y);

            selected = CameraOperator.selection.Contains(camPos);


            if (selected)

                GetComponent().material.color = Color.green;

            else

                GetComponent().material.color = Color.white;

        }


        if (selected && Input.GetMouseButtonUp(1))

        {

            Vector3 destination = CameraOperator.GetDestination();


            if (destination != Vector3.zero)

            {

                //gameObject.GetComponent().SetDestination(destination);


                moveToDest = destination;

                moveToDest.y += floorOffset;


            }

        }


        UpdateMove();


    }

    private void UpdateMove()

    {

        if (moveToDest != Vector3.zero && transform.position != moveToDest)

        {

            Vector3 direction = (moveToDest = transform.position).normalized;

            direction.y = 0;

            transform.GetComponent().velocity = direction * speed;


            if (Vector3.Distance(transform.position, moveToDest)

                moveToDest = Vector3.zero;

        }

        else

            transform.GetComponent().velocity = Vector3.zero;

        

    }

}


유닛 드래그클릭하고 선택된유닛 초록색 실시간업데이트 등등은 다되는데 움직이는것만안되...