Impossible to beat Level 4!

Rocket Escape!–Click Here and Click Full Screen

Keys

Space = boost
A = rotate left
D = rotate right

Goal

Fly the rocket onto the green landing pad without hitting anything.

Bug

On level 4 there is a moving wall that chases the player. The wall has the moving object code from the project boost section. As long as the wall starts at its beginning point the player should have enough time to beat the level before the wall destroys them. If I start from level 4 immediately the object starts in the right place…however…sometimes when the player starts from the beginning of the game (Level 1) and reaches level 4 the wall doesn’t start at its beginning point and at a random point along the moving path. This can cause the player to instantly die when the level starts. It seems like the object keeps moving along its path even when the player is on a different level. Not sure how to fix this…

Hi Kirt,

The object does not start from a random position. Your algorithm uses Time.time to calculate the position. When you click the play button, Time.time is 0. You need to make sure that the algorithm in your instance starts at 0, too.

This can be achieved by creating a new float variable. Name it offset or whatever makes sense to you. In the Awake() method, assign Time.time to it.

This is a part of the solution. Try to figure out the rest. It’s simple if you read my first paragraph again.

If you cannot solve this by yourself, please let me know.

Hi Nina,

Thanks for the response. I tried to follow what you wrote, but I had a hard time figuring out what needed to be done. I don’t have the awake() method in my code. See below. I could add it in, but then I’m not really sure how to implement the rest of what you were talking about. Is there a way to reset the time to zero at the start of each level? Or is there a better way to do this?

[DisallowMultipleComponent]
public class Oscillator : MonoBehaviour
{
    [SerializeField] Vector3 movementVector = new Vector3(10f,10f, 10f);
    [SerializeField] float period = 2f;

    float movementFactor; // 0 for not moved, 1 for fully moved.
    Vector3 startingPos;

    // Start is called before the first frame update
    void Start()
    {
        startingPos = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        // protect against period is zero
            if (period <= Mathf.Epsilon) { return; }
            float cycles = Time.time / period; //grows continually from 0

            const float tau = Mathf.PI * 2f; // about 6.28
            float rawSinWave = Mathf.Sin(cycles * tau); // goes from -1 to +1

            movementFactor = rawSinWave / 2f + 0.5f;
            Vector3 offset = movementVector * movementFactor;
            transform.position = startingPos + offset;
    }
}

Unfortunately, I cannot see where you implemented it but maybe I’m currently blind. Time.time cannot be overridden.

This is what I meant:

float timeAtStart;

void Start ()
{
    timeAtStart = Time.time;
}

void Update()
{
    if (period <= Mathf.Epsilon) { return; }
    float cycles = (Time.time - timeAtStart) / period; //grows continually from 0
    // rest of your code
}

Thanks that worked perfect. That makes sense the way you explained it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms