Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by nod786 · Jul 24, 2014 at 03:28 PM · saveloadliferestrictrestrictionsrestrictmovement

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);

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges