Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Im working on 2 D platfomer in UNity using C# . I created a levelmanager script to load each level and save player progress. However,

Im working on 2D platfomer in UNity using C#. I created a levelmanager script to load each level and save player progress. However, im now trying to add a checkpoint system, with the last cehckpoint activated also saved. Heres my levelmanager code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LevelManager : MonoBehaviour
{
//declare levelsUnlocked
int levelsUnlocked;
//declare checkpointsUnlocked
int checkpointsUnlocked;
//holds the transform value
public Vector3 lastCheckPointActivated;
//declare boolean levelWon and set to false
bool levelWon = false;
//need fixing a placeholder value
int playerStartPos =0;
//levelWon function
public void levelIsWon()
{
//set levelWon to true
levelWon = true;
}
//start function
void Start()
{
//sets levelsUnlocked to integer stored in Player preferences. If no int is stored the value is 1
levelsUnlocked = PlayerPrefs.GetInt("levelsUnlocked",1);
//check last checkpoint unlocked.If no integer is store set equal to 0
checkpointsUnlocked = PlayerPrefs.GetInt("checkpointsUnlocked",0);
//calls levelLoad function to load the level indicated by levelsUnlocked
levelLoad();
checkpointLoad();
}
//loadLevel function
public void loadLevel(int levelNum)
{
//load the level
SceneManager.LoadScene(levelNum);
}
//update function
void Update()
{
//if levelWon equals true
if (levelWon == true)
{
checkpointsUnlocked =0;
//and if levelsUnlocked is less than 9
if (levelsUnlocked <9)
{
//increment levelsUnlocked by one
levelsUnlocked++;
//save new value to levelsUnlocked
PlayerPrefs.SetInt("levelsUnlocked", levelsUnlocked);
//set checkpoint to 0
PlayerPrefs.GetInt("checkpointsUnlocked",0);
}
else if (levelsUnlocked >9)
{
//load the credits scene
SceneManager.LoadScene(10);
}
//sets levelWon back to false
levelWon = false;
}
}
//level load function
void levelLoad()
{
//if levelsUnlocked is less than or equal to 1
if (levelsUnlocked >=1)
{
//load level 1
SceneManager.LoadScene(1);
}
//else if levelsUnlocked equals 2
else if (levelsUnlocked ==2)
{
//load level 2
SceneManager.LoadScene(2);
}
//else if levelsUnlocked equals 3
else if (levelsUnlocked ==3)
{
//load level three
SceneManager.LoadScene(3);
}
//else if levelsUnlcoked equals 4
else if (levelsUnlocked ==4)
{
//load level 4
SceneManager.LoadScene(4);
}
//else if levelsUnlocked equals 5
else if (levelsUnlocked ==5)
{
//load level five
SceneManager.LoadScene(5);
}
//else if levelsUnlocked equals 6
else if (levelsUnlocked ==6)
{
//load level six
SceneManager.LoadScene(6);
}
//else if levelsUnlocked equals 7
else if (levelsUnlocked ==7)
{
//load level seven
SceneManager.LoadScene(7);
}
//else if levelsUnlocked equals 8
else if (levelsUnlocked ==8)
{
//load level eight
SceneManager.LoadScene(8);
}
//else if levelsUnlocked equals 9
else if (levelsUnlocked ==9)
{
//load level nine
SceneManager.LoadScene(9);
}
}
void checkpointLoad()
{
//if checkpointsUnlocked equals 0
if (checkpointsUnlocked ==0)
{
//player starts at beginning of level
// playerStartPos ==(0);
}
//if checkpointsUnlocked equals 1
else if (checkpointsUnlocked ==01)
{
//player starts at checkpoint1
}
//if checkpointsUnlocked equals 2
else if (checkpointsUnlocked ==02)
{
//player position is at checkpoint 2
}
//else if checkpointsUnlocked equals 3
else if (checkpointsUnlocked ==03)
{
//player position is at checkpoint 3
}
//else if checkpointsUnlocked equals 4
else if (checkpointsUnlocked ==04)
{
//player pos is at checkpoint4
Here is my checkpoint code thus far:
public class Checkpoint : MonoBehaviour
{
private LevelManager lm;
void onTriggerEnter2D(Collider2D other)
{
//if player collides w
Im a litle rusty so any guidance would be appreciated

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application And Administration

Authors: Michael Mannino, Michael V. Mannino

2nd Edition

0072880678, 9780072880670

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago