using UnityEngine;

public class PointMove : MonoBehaviour
{
    //public Transform dummy;

    public float speed = 8f;        // Moviment Velocity
    Vector3 direction;                  // PointCLick Direction
    Vector3 click_position;             //Mouse Position on click

    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            RaycastHit hit;
            Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit);
            if (hit.point!=null) click_position = hit.point;
            direction = click_position - transform.position;
        }
        //dummy.position = click_position;

        if (Vector3.Distance(click_position, transform.position) > 0.5f)
        {
            GetComponent<Animator>().SetFloat("speed", speed);
            var p = Vector3.MoveTowards(transform.position, click_position, speed * Time.deltaTime);
            GetComponent<Rigidbody>().MovePosition(p);
            //transform.position = p;
        }
        else GetComponent<Animator>().SetFloat("speed", 0);
    }

}
