How to pause a game with way point progress tracker

Hi I just wonder how you can make pause button for a game like Argon Assault Where waypoint progress tracker script is enabled in the main character object . I have looked at a lot of tutorial online however it does not work . I know that I have to set TimeScale=0 . If I disable the script WayPoint Progress Tracker then the game pause as expected. However , if the script is enabled, I cant pause the game, the main character just keep flying.

Hello Minh,

Instead of disabling the script you could use a combination of Time.timescale and also set the lookAheadForTargetOffset and lookAheadForTargetFactor to zero. This is how far ahead of the route the script is interested in, so by setting it to zero to effectively don’t move.

There are obviously a number of ways you could do this, some will be better than others but as Unity’s Timeline is used later in the section you might not want to spend too much time on it.

If you want to have a play with this, try the following;

In WaypointProgressTracker.cs set the the two variables to be public instead of private;

[SerializeField] public float lookAheadForTargetOffset = 5;
// The offset ahead along the route that the we will aim for

[SerializeField] public float lookAheadForTargetFactor = .1f;
// A multiplier adding distance ahead along the route to aim for, based on current speed

In PlayerController.cs add a member variable that can hold a reference to the WaypointProgressTracker component on the camera and a bool to hold the current pause state;

[SerializeField] WaypointProgressTracker waypointProgressTracker;

bool isPaused = false;

Within the Update method, add this;

        if (CrossPlatformInputManager.GetButton("Pause"))
        {
            if (!isPaused)
            {
                isPaused = true;
                Time.timeScale = 0;

                waypointProgressTracker.lookAheadForTargetOffset = 0f;
                waypointProgressTracker.lookAheadForTargetFactor = 0f;

                Debug.Log("Game Is Paused");
                Debug.Log("timescale : " + Time.timeScale);
            }
            else
            {
                isPaused = false;
                Time.timeScale = 1;

                waypointProgressTracker.lookAheadForTargetOffset = 1f;
                waypointProgressTracker.lookAheadForTargetFactor = 0.1f;

                Debug.Log("Game Is Not Paused");
                Debug.Log("timescale : " + Time.timeScale);
            }
        }

Now select the PlayerShip in the Hierarchy, and in the exposed Waypoint Progress Tracker field, drag the Main Camera in.

image

Under the Edit menu, select Project Settings / Input, increase the Axes array size to 19 from 18. Select the duplicated Cancel and set it’s details as follows;

image

This will enable you to use the CrossPlatformInputManager, used in the Update method code above.

Run the game, press “p” to Pause. You should see the ship top flying and everything else in the scene stop too.

Note, the above is a rather rushed and messy solution, certainly not something I would want to implement, but hopefully it will give you a basis to build from, but as I say, Timeline is used later in this section so it may be worth holding fire until you complete the section, then go back in to add features.

Hope this helps :slight_smile:

2 Likes

Thank you very much. It really help. I can pause the game now. I would really appreciate if you can take a look at How to make the ship jump and give me some more advices as I would like to add “jump” feature for the ship

1 Like

You’re very welcome :slight_smile:

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

Privacy & Terms