using HoloToolkit.Unity.SpatialMapping;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TapToPlace_MoveUpByHalfHeight : TapToPlace {


	protected override void Update () {
        // If the user is in placing mode,
        // update the placement to match the user's gaze.
        if (IsBeingPlaced) {
            // Do a raycast into the world that will only hit the Spatial Mapping mesh.
            Vector3 headPosition = Camera.main.transform.position;
            Vector3 gazeDirection = Camera.main.transform.forward;

            RaycastHit hitInfo;
            if (Physics.Raycast(headPosition, gazeDirection, out hitInfo, 30.0f, spatialMappingManager.LayerMask)) {
                // Rotate this object to face the user.
                Quaternion toQuat = Camera.main.transform.localRotation;
                toQuat.x = 0;
                toQuat.z = 0;

                float halfY = (gameObject.GetComponent<Collider>().bounds.size.y) / 2;
                Vector3 pos = hitInfo.point;
                pos.y = hitInfo.point.y + halfY;

                // Move this object to where the raycast
                // hit the Spatial Mapping mesh.
                if (PlaceParentOnTap) {
                    // Place the parent object as well but keep the focus on the current game object
                    Vector3 currentMovement = hitInfo.point - gameObject.transform.position;
                    ParentGameObjectToPlace.transform.position += currentMovement;
                    ParentGameObjectToPlace.transform.rotation = toQuat;
                } else {
                    gameObject.transform.position = pos;
                    gameObject.transform.rotation = toQuat;
                }
            }
        }
    }
}
