public void StartNewDrag()
 {
     selectedWall       = WallUnderMouse(activatedCamera, wallsLayer);
     originWallPosition = selectedWall.getPosition();
     originWallZRot     = selectedWall.getZRotation();
     isDragging         = true;
 }
    public EditorWall WallUnderMouse(Camera camera, LayerMask layer)
    {
        RaycastHit[] hits;
        RaycastHit   hit;
        float        lowestDistanceToCurrentBeat = 100f;
        float        currentUnit = Track.GetUnitByMeasure(Track.CurrentSelectedMeasure);
        Ray          ray         = camera.ScreenPointToRay(Input.mousePosition);

        hits = Physics.RaycastAll(ray, 50f, layer);
        if (hits.Length < 1)
        {
            return(new EditorWall()); // If nothing is hit, do nothing
        }
        hit = hits[0];                // Start by assuming the first hit is the closest
        // Check for closer hits
        for (int i = 0; i < hits.Length; i++)
        {
            //Debug.Log("Hit wall name: " + hits[i].transform.gameObject.name);
            float distanceToCurrentBeat = Mathf.Abs(hits[i].transform.position.z - currentUnit);
            // Check if hit is closer, filtering anything prior to the current beat (with small tolerance added to accomodate rounding issues)
            if (distanceToCurrentBeat < lowestDistanceToCurrentBeat && (hits[i].transform.position.z + .001) >= currentUnit)
            {
                lowestDistanceToCurrentBeat = distanceToCurrentBeat;
                hit = hits[i];
            }
        }
        //Debug.Log("Chosen wall name: " + hit.transform.gameObject.name);
        if ((hit.transform.position.z + .001) < currentUnit)
        {
            return(new EditorWall());                                                     // If no hits were at or after the current beat, do nothing
        }
        Renderer   rend          = hit.transform.GetComponent <Renderer>();
        EditorWall draggableWall = new EditorWall();

        draggableWall.wallGO = hit.transform.parent.gameObject;
        mouseWallOffset      = hit.point - draggableWall.wallGO.transform.position;
        if (draggableWall.wallGO != null)
        {
            draggableWall.slide = Track.TryGetSlideAtPositionZ(draggableWall.wallGO.transform.position.z);
            if (draggableWall.slide.initialized)
            {
                draggableWall.exists = true;
                draggableWall.time   = draggableWall.slide.time;
                return(draggableWall);
            }
            else
            {
                draggableWall.crouch = Track.TryGetCrouchAtPositionZ(draggableWall.wallGO.transform.position.z);
                if (draggableWall.crouch.initialized)
                {
                    draggableWall.exists   = true;
                    draggableWall.isCrouch = true;
                    draggableWall.time     = draggableWall.crouch.time;
                    return(draggableWall);
                }
            }
        }
        return(new EditorWall());
    }
    public bool isWallUnderMouse()
    {
        EditorWall checkedWall = WallUnderMouse(activatedCamera, wallsLayer);

        if (checkedWall != null && checkedWall.exists && checkedWall.wallGO != null)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
    public Vector3 getWallUnderMousePosition()
    {
        EditorWall checkedWall = WallUnderMouse(activatedCamera, wallsLayer);

        if (checkedWall != null && checkedWall.exists && checkedWall.wallGO != null)
        {
            return(checkedWall.wallGO.transform.position);
        }
        else
        {
            return(new Vector3(0, 0, 0));
        }
    }
    private void EndCurrentDrag()
    {
        if (!selectedWall.exists)
        {
            return;
        }
        Vector3 finalPos = selectedWall.wallGO.transform.position;
        Vector3 finalRot = selectedWall.wallGO.transform.GetChild(0).eulerAngles;

        selectedWall.setPositionRotation(new float[3] {
            finalPos.x, finalPos.y, finalPos.z
        }, finalRot.z);

        Track.FinalizeWallDrag(selectedWall.time, selectedWall.getType(), originWallPosition, selectedWall.getPosition(), originWallZRot, selectedWall.getZRotation());
        Track.HistoryChangeDragWall(selectedWall.getType(), selectedWall.time, originWallPosition, selectedWall.getPosition());
        selectedWall       = new EditorWall();
        originWallPosition = new float[] { 0, 0, 0 };
        originWallZRot     = 0f;
        mouseWallOffset    = Vector3.zero;
        isDragging         = false;
    }