IEnumerator PreviewBallTrajectory()
    {
        Vector3 prevCursorPosition = Vector3.zero;
        Vector3 cursorPosition;
        Vector3 cursorScreenPosition;

        while (selectedRobot != null && ball != null)
        {
            cursorPosition       = Input.mousePosition;
            cursorScreenPosition = Camera.main.ScreenToWorldPoint(cursorPosition);
            if (prevCursorPosition != cursorPosition)
            {
                if (ballMovingTrails != null && ballMovingTrails[selectedRobotIndex].Count > 0)
                {
                    Destroy(ballMovingTrails[selectedRobotIndex].First().TrailGameObject);
                    ballMovingTrails[selectedRobotIndex].Clear();
                }
                MoveCommand emptyMoveCommand = new MoveCommand(ball, Vector2.zero, 0, turns);
                latestBallTrail = new MovingTrail(emptyMoveCommand, timeInput, ball.GetComponent <Ball>().PreviousVelocity);
                ballMovingTrails[selectedRobotIndex].Add(latestBallTrail);
            }
            prevCursorPosition = cursorPosition;
            yield return(new WaitForSeconds(0.005f));
        }
    }
 void DestroyPreviewTrails()
 {
     if (latestRobotTrail != null)
     {
         Destroy(latestRobotTrail.TrailGameObject);
     }
     latestRobotTrail = null;
 }
    IEnumerator PreviewTrajectoryAndGiveRobotCommand()
    {
        Vector3 prevCursorPosition   = Vector3.zero;
        Vector3 cursorPosition       = Input.mousePosition;
        Vector3 cursorScreenPosition = Camera.main.ScreenToWorldPoint(cursorPosition);
        Command previewCommand       = null;

        GameObject lastRobot = null;
        GameObject previewRobot;
        float      timeBetweenPreivews = 0.1f;

        System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
        Command.AvailableCommands    prevSelectedCommand = selectedCommand;

        timer.Start();
        while (selectedRobot != null)
        {
            cursorPosition       = Input.mousePosition;
            cursorScreenPosition = Camera.main.ScreenToWorldPoint(cursorPosition);
            if (timeInput <= selectedRobot.GetComponent <RobotBehaviour>().freeTime&& timer.Elapsed.TotalSeconds > timeBetweenPreivews)
            {
                if (prevCursorPosition != cursorPosition || prevSelectedCommand != selectedCommand || RevertCommand() || lastRobot != selectedRobot)
                {
                    timer.Reset();
                    timer.Start();
                    DestroyLatestPreviewTrail();
                    if (robotMovingTrails[selectedRobotIndex].Count > 0)
                    {
                        previewRobot = robotMovingTrails[selectedRobotIndex].Last().Node;
                    }
                    else
                    {
                        previewRobot = selectedRobot;
                    }
                    cursorPosition = Input.mousePosition;

                    cursorScreenPosition = Camera.main.ScreenToWorldPoint(cursorPosition);

                    if (selectedCommand == Command.AvailableCommands.Move)
                    {
                        previewCommand = new MoveCommand(previewRobot, cursorScreenPosition, timeInput, Turns);
                    }
                    else if (selectedCommand == Command.AvailableCommands.Push && timeInput <= selectedRobot.GetComponent <RobotBehaviour>().freeTime - shockWavePrefab.GetComponent <ShockwaveBehaviour>().intendedLifetime)
                    {
                        previewCommand = new PushCommand(previewRobot, cursorScreenPosition, timeInput, Turns);
                    }
                    else
                    {
                        Debug.Log("No command selected!");
                    }
                    latestRobotTrail = new MovingTrail(previewCommand, timeInput, previewRobot.GetComponent <RobotBehaviour>().prevVelocity);

                    //Not done yet!
                    //if (latestRobotTrail != null && latestRobotTrail.Node != null)
                    //{
                    //    directionPointer = Instantiate(Resources.Load<GameObject>("Prefabs/Prototype stuff (remove or rework)/Direction Arrow")) as GameObject;
                    //    //directionPointer.transform.position = latestBallTrail.Node.transform.position;
                    //    //directionPointer.transform.parent = latestBallTrail.Node.transform;
                    //    float AngleRad = Mathf.Atan2(cursorPosition.y - directionPointer.transform.position.y, cursorPosition.x - directionPointer.transform.position.x);
                    //    float AngleDeg = (180 / Mathf.PI) * AngleRad;
                    //    directionPointer.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);

                    //    directionPointer.name = "Direction Pointer";

                    //}
                }
                if (Input.GetMouseButtonDown(1) && latestRobotTrail != null)
                {
                    latestRobotTrail.TrailGameObject.transform.parent = movingPreviews[selectedRobotIndex].transform;
                    robotMovingTrails[selectedRobotIndex].Add(latestRobotTrail);
                    latestRobotTrail = null;

                    GiveRobotCommand(previewCommand);
                }
            }
            if (selectedRobot != lastRobot)
            {
                DestroyLatestPreviewTrail();
            }
            prevCursorPosition  = cursorPosition;
            prevSelectedCommand = selectedCommand;
            lastRobot           = selectedRobot;
            yield return(new WaitForSeconds(0.005f));
        }
        DestroyLatestPreviewTrail();
    }