Here's my solution to the challenge

Maybe not the prettiest solution but here’s what I threw together to make the block move back and forth

[SerializeField] Vector3 movementVector;
    [Range(0, 1)] [SerializeField] float movementFactor;
    Vector3 startingPos;
    bool reachedMax = false;
    float speedFactor = 0.25f;

	// Use this for initialization
	void Start ()
    {
        startingPos = transform.position;
	}

void Update ()
    {
        AdjustMovementFactor();
        Vector3 offset = movementVector * movementFactor;
        transform.position = startingPos + offset;
	}

void AdjustMovementFactor()
    {
        if (movementFactor == 1.0f)
            reachedMax = true;

        if (!reachedMax)
            movementFactor += Mathf.Min(Time.deltaTime * speedFactor, 1.0f - movementFactor);
        else
        {
            movementFactor -= Mathf.Min(Time.deltaTime * speedFactor, movementFactor);
            if (movementFactor == 0.0f)
                reachedMax = false;
        }
    }

I also wrote some code to make sure the rocket is standing upright on the landing platform before winning and make the rocket die if it lands too hard

in update:

if (landing)
            {
                DetermineLandingResult();
            }

in OnCollisionEnter:

case "Finish":
                if (rb.velocity.y > maxLandingVel)
                    landing = true;
                else
                    StartDeathSequence();
                    break;

and the DetermineLandingResult()

void DetermineLandingResult()
    {
        if (transform.up.y == 1.0f)
        {
            StartSuccessSequence();
        }
    }

Tweaked it a bit after the lecture on epsilons

float y = transform.up.y;
       if (y >= 1.0f - Mathf.Epsilon && y <= 1.0f + Mathf.Epsilon)
       {
           StartSuccessSequence();
       }

Privacy & Terms