- Home /
adding a savable restrictions
Hi guys sorry if this has been asked before. Im trying to work out how to add restrictions that will be saved in the game memory so they don't reset every time you start a new game.
Aim -
Double jump. My game has double jump in it, but I want the user to have a x number of jumps to start of with lets say 50. so for every double jump the user does the number should go down, but for every 10 single jumps a user does the number should go up by 1 and it should go to a maximum of 99 double jumps.
Life restriction - this is the other thing I am stuck on I want the user to start with 5 lives and and for every 100 single jumps he get 1 life to a maximum of 99 lives.
if anyone can help me that would be awesome been stuck on this for a while.
Ill add the code im using below. thanks guys!
using UnityEngine; using System.Collections;
public class DoubleJumperManagerScript : MonoBehaviour {
public static int dJumpLimit = 30;
void Start()
{
PlayerPrefs.SetInt ("DoubleJumps", dJumpLimit);
}
void Update()
{
PlayerPrefs.SetInt ("DoubleJumps", CharacterMove.dJumpLimit);
}
}
then printing double jumps remaining on scene
using UnityEngine; using System.Collections;
public class DoubleJumpScript : MonoBehaviour {
//CharacterMove dJumps;
public int doubleJumps;
void Update()
{
doubleJumps = PlayerPrefs.GetInt ("DoubleJumps");
this.guiText.text = "Double Jumps: " + doubleJumps;
}
}
finally the player script which decreases and increases double jumps and will disable or enable depending on doubles jumps remaining
using UnityEngine; using System.Collections;
public class CharacterMove : MonoBehaviour {
public float speed = 6.0f;
Transform groundCheck;
private float overlapRadius = 0.2f;
public LayerMask whatIsGround;
private bool grounded = false;
private bool jump = false;
public float jumpForce = 700f;
private bool doubleJump = false;
public int sJumpsCompleted = 0;
public static int dJumpLimit = 0;
void Awake()
{
rigidbody2D.fixedAngle = true;
PlayerPrefs.SetInt ("DoubleJumps", DoubleJumperManagerScript.dJumpLimit);
dJumpLimit = PlayerPrefs.GetInt ("DoubleJumps");
}
void Start()
{
groundCheck = transform.Find ("groundcheck");
}
void Update()
{
if (Input.GetMouseButtonDown (0))
{
jump = true;
sJumpsCompleted++;
}
if (sJumpsCompleted > 9)
{
dJumpLimit = dJumpLimit + 3;
PlayerPrefs.SetInt ("DoubleJumps", dJumpLimit);
sJumpsCompleted = 0;
}
}
void FixedUpdate()
{
//check if character is grounded
grounded = Physics2D.OverlapCircle (groundCheck.position, overlapRadius, whatIsGround);
//reset doublejump when player is on the ground
if (grounded)
doubleJump = false;
//deactivate double jump when no jumps are available
if (dJumpLimit < 1)
doubleJump = true;
//determine if player can jump
bool canJump = (grounded || !doubleJump);
if (jump && canJump)
{
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x,0);
rigidbody2D.AddForce(new Vector2(0, jumpForce));
if(!grounded && DoubleJumperManagerScript.dJumpLimit > 0)
{
doubleJump = true;
dJumpLimit--;
PlayerPrefs.SetInt ("DoubleJumps", dJumpLimit);
}
}
jump = false;
//apply forward movement
rigidbody2D.velocity = new Vector2 (speed, rigidbody2D.velocity.y);
}
}
Your answer
Follow this Question
Related Questions
Restrict the download of an app through password? (ANDROID) 2 Answers
How to create a restricted mesh system? 1 Answer
Lives dont minus correctly 1 Answer
Player Health Not Working? 4 Answers
How to truly lock an axis in a configurable joint? 2 Answers