Why does my game repeat its restart method at the beginning of the game? :(

Hi, class how can I stop the game from restarting at the beginning of the game? Any insight is very appreciated :confused:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LandingPad: MonoBehaviour
{

}


 private bool landed = true;
    private GameController gameController;



    // Update is called once per frame
    void Update()
    {
        if (isControlEnabled)
        {
            ProcessTranslation();
            ProcessRotation();
            ProcessFiring();
        }

    }
    void OnPlayerDeath()
    {
        isControlEnabled = false;
        gameController.GameOver();
    }
    private void Start()
    {
        gameController = FindObjectOfType<GameController>();
    }

    private void ProcessRotation()
    {
        float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
        float pitchDueToControlThrow = yThrow * controlPitchFactor;
        float pitch = pitchDueToPosition + pitchDueToControlThrow;
        float yaw = transform.localPosition.x * positionYawFactor;
        float roll = xThrow * controlRollFactor;

        transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
    }

    private void ProcessTranslation()
    {

        xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
        yThrow = CrossPlatformInputManager.GetAxis("Vertical");

        float xOffset = xThrow * controlSpeed * Time.deltaTime;
        float yOffset = yThrow * controlSpeed * Time.deltaTime;


        float rawXPos = transform.localPosition.x + xOffset;
        float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);

        float rawYPos = transform.localPosition.y + yOffset;
        float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);

        transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
    }
    void OnPlayerLanding()
    {
        isControlEnabled = false;

        if (!landed)
        {
            gameController.GameOver();
        }
    }

    void OnPlayerTakeOff()
    {
        isControlEnabled = true;
        landed = false;
    }

    void ProcessFiring()
    {
        if (CrossPlatformInputManager.GetButton("Fire"))
        {
            SetGunsActive(true);
        }
        else
        {
            SetGunsActive(false);
        }
    }
    private void SetGunsActive(bool isActive)
    {
        foreach (GameObject gun in guns) // care may affect death FX
        {
            var emissionModule = gun.GetComponent<ParticleSystem>().emission;
            emissionModule.enabled = isActive;
        }
    }
}


Hi Martin,

This problem only occurs if you actually move the player ship during take off, or rather, try to. What is happening is that you move up during take off (via the Timeline) and the moment you exit the BoxCollider on the LandingPad we are assuming take off is good, but if the player move the ship left, right or down fairly quickly, the player makes contact with the BoxCollider on the LandingPad again and, because we’ve assumed the next time they did it would be at the end of the game, the Game Over condition is met.

void OnPlayerLanding()
{
    isControlEnabled = false;

    if (!landed)
    {
        gameController.GameOver();
    }
}

There are better ways to resolve this issue, but it’s getting increasingly difficult to write good solutions in the existing code, but the following will work for now;

Update LandingPad.cs

using UnityEngine;

public class LandingPad : MonoBehaviour
{
    private void OnTriggerExit(Collider other)
    {
        PlayerController playerController = other.gameObject.GetComponent<PlayerController>();

        if (playerController)
        {
            gameObject.GetComponent<BoxCollider>().enabled = false;

            Invoke("ActivateLandingPad", 10f);
        }
    }

    private void ActivateLandingPad()
    {
        gameObject.GetComponent<BoxCollider>().enabled = true;
    }
}

What the above does is to catch the moment the player ship has left he BoxCollider and then it disabled the BoxCollider, as such, any player movement doesn’t trigger another collision.

The downside of this approach is that because you are relying on that BoxCollider for triggering the end of the game it needs to be turned back on again. That’s what the Invoke method is doing. it calls the ActivateLandingPad after a rather arbitrary period of time - 10 seconds - I set it to this because this should be enough time for the player ship to be moved away from the LandingPad by Timeline, but its less than the duration to get around the entire route of the rails, thus, it’s back on before you come back around to it.

This is fairly messy as solutions go - but it does work and will perhaps buy you some time until a better solution is in place.

Hope this helps :slight_smile:

1 Like

I will try this Thank you. Have you tried the game since then?

I haven’t Martin, no. Hope you’re enjoying developing it.

1 Like

Hi, Rob, I’m very excited about getting the new assets from Daz3D I think it will work out just fine. I hope if I get stuck I can reach out to you if you can spare the time. I plan to make the player get out of a new ship from Daz3D assets store I will buy. and I will create a new scene with at least 8 levels where the player is walking or running around as an fps game shooting aliens I will buy at the Daz3D asset store. in three weeks I will buy some of the assets and terrain scenes will show when I have it. Thank you for all you do. :slight_smile:

1 Like

Not wishing to kill any enthusiasm but I would really focus on tightening up/improving what you already have before trying to add any further complexity to it. The course content delivers a very specific outcome, not one that is particularly finished, as you’ve already discovered with regards to how to loop around or end the level. These are the kind of things that should be considered at the start before heading down a path of writing code.

One thing you could consider, which may make it easier, would be to consider creating these new features in a separate project, that way you can experiment with the new features, overcome whatever problems you encounter without the existing game’s code base. Then, when you’re ready you could look at bringing those features in to the main game. Just a thought.

Yes!! thoughs are good ideas it would be better to create a separate project. Then fix this project with what I discovered went wrong.Thank you

1 Like

Privacy & Terms