Programming the Main Menu

This post explains how the main menu of Veclicous racing works. The main menu scene is just a few props and a UI canvas, but I think the visual effect it quite powerful. Below is a video showing it functioning.

A screen shot of the main menu in the editor is shown below,

The only programming that is contained within the main menu scene is that programming which makes the main menu move and of course the logic to make the buttons function. I will first explain the movement of the main menu when a player ever the player clicks certain button.

The main menu is contained on a single canvas, so if this canvas is moved all of the buttons on the canvas will be subsequently  moved. This is essentially how the main menu of Velicous racing function, when the player presses a certain button, the canvas is moved directly up or down to reveal new buttons

The picture below attempts to illustrate the programming of the main menu.

mainmenu4

A screen shot of the in engine Editor also shows the setup.

mainmenu5

As usual I provide a flow char to help explain the later code.

New-Mind-Map (14)

Pasted Raw Code,

public class Main_Menu_Manager_Script : MonoBehaviour {

 public GameObject MenuHolder;
 public GameObject LoadingScreen;
 public Slider music;
 public Slider effects;
 private GameObject Target;

 void Start()
 {
  music.value = PlayerPrefs.GetFloat("MusicVolume");
  effects.value = PlayerPrefs.GetFloat("SoundEffectVolume");
 }

 void Update()
 {
  if (Target != null)
  {
   float distance = Vector3.Distance(MenuHolder.transform.position, Target.transform.position);
   MenuHolder.transform.position = Vector3.Lerp(MenuHolder.transform.position, Target.transform.position, 10 * Time.fixedDeltaTime);
   if (distance < 10)
   {
    Target = null;
   }
  }
 }

 public void ChangeSoundEffectVolume(float _value)
 {
  PlayerPrefs.SetFloat("SoundEffectVolume", _value);
 }

 public void ChangeSoundMusicVolume(float _value)
 {
  PlayerPrefs.SetFloat("MusicVolume", _value);
 }

 public void Quit()
 {
  Application.Quit();
 }

 public void MoveMenu(GameObject _target)
 {
  Target = _target;
 }

 public void ChangeLevel(int _level)
 {
  LoadingScreen.SetActive(true);
  SceneManager.LoadScene(_level);
 }

 void OnEnable()
 {
  LoadingScreen.SetActive(false);
 }

 public void ClearSave()
 {
  PlayerPrefs.DeleteAll();
 }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: