Exemplo n.º 1
0
    private IEnumerator MoveTorward(Transform startPos, float duration)
    {
        //Get the position 1 unit towards the player
        moveToPos = transform.position + Vector3.back;
        cubeInfo.SetCoord((int)moveToPos.x, (int)-moveToPos.z);
        Vector3 rotDir = transform.right;

        if (isMoving)
        {
            yield break;
        }
        isMoving = true;
        //Get the rotation speed (Angle to rotate/ time to rotate)
        float      rotSpeed   = -90 / duration;
        float      counter    = 0;
        float      deltaAngle = 0;
        Vector3    currentPos = startPos.position;
        Quaternion startRot   = transform.rotation;

        while (counter < duration)
        {
            //Slowly increment the delta angle for rotation
            deltaAngle += rotSpeed * Time.deltaTime;
            //make sure the delta angle is smaller than the overall angle that is to be rotated
            deltaAngle = Mathf.Min(deltaAngle, -90);
            //Increment the counter using delta time
            counter += Time.deltaTime;
            //Slowly move the cube towards the player
            transform.position = Vector3.Lerp(currentPos, moveToPos, counter / duration);
            //slowly rotate the cube towards the play to make it look like its moving towards the player
            transform.rotation = startRot * Quaternion.AngleAxis(deltaAngle, rotDir);
            yield return(null);
        }
        CheckIfCubeIsPastLevelLength();
        //Makes sure the rotation is a perfect 90°
        transform.rotation = startRot * new Quaternion(-90, 0, 0, 0);
        isMoving           = false;
        //if the cube has been deactivated halfway through moving, move down after finished moving
        if (!cubeInfo.isActive)
        {
            StartCoroutine(MoveDownAtDiactivate(timeToGetToStartPos));
        }
    }