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

public class FloatingScript : MonoBehaviour
{
    //tric jo mainass tuvaka virsotne:(
    Transform SeaPlane;
    Cloth PlaneCloth;
    int ClosestVertex = -1;
    float ShipStarHeight;
    float SphereStarHeight;
    GameObject TestSphere;

	void Start ()
    {
        SeaPlane = GameObject.Find("Sea").transform;
        PlaneCloth = SeaPlane.GetComponent<Cloth>();
        ShipStarHeight = transform.localPosition.y;
        TestSphere = GameObject.Find("TestSphere");
        SphereStarHeight = TestSphere.transform.localPosition.y;

    }

    void Update ()
    {
        GetClosestVertex();
	}

    void GetClosestVertex()
    {
        for (int i = 0; i < PlaneCloth.vertices.Length; i++)
        {
            if (ClosestVertex == -1)
            {
                ClosestVertex = i;
            }

            float Distance = Vector3.Distance(PlaneCloth.vertices[i], transform.position);
            float ClosestDistance = Vector3.Distance(PlaneCloth.vertices[ClosestVertex], transform.position);
            
            if (Distance < ClosestDistance)
            {
                ClosestVertex = i;
                //Debug.Log("shorter"+ Distance);
            }
        }
        
        //TestSphere.transform.position = PlaneCloth.vertices[ClosestVertex];
        //TestSphere.transform.position = new Vector3(TestSphere.transform.position.x, SphereStarHeight + PlaneCloth.vertices[ClosestVertex].y, TestSphere.transform.position.z);
        //Debug.Log("Y: "+PlaneCloth.vertices[ClosestVertex].y);
        transform.localPosition = new Vector3(transform.localPosition.x, PlaneCloth.vertices[ClosestVertex].y/5, transform.localPosition.z);

    }

}
