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 /
avatar image
0
Question by Kebabs · Feb 14, 2014 at 10:09 PM · mathfmathf.clamp

What am I doing wrong? Using mathf.clamp to cap an int value. value

Hi. This may be obvious to some however being relatively new to using C# I am finding it hard sometimes to achieve something simple. I am making a stamina system for my project/game. I have it decreasing and increasing which is great, but it increases past the a point I wish to stop it at.

I want my stamina in this case to be capped at 200 but it recovers way beyond that. I also want my stamina NOT to fall below 0 but it seems to if I hold shift long enough. I think using mathf.clamp is the right solution but sure on the execution of it.

My problem lies in the (canRegen). I want stamina to recover at a steady 60 units a second up-to 200 and then stop. I assume the same could be used to have a min for stamina of 0 so that it cannot fall below 0.

Any help is greatly appreciated as always as this is for uni. Thank you for your time.

using UnityEngine; using System.Collections;

public class stamina : MonoBehaviour { public static float maxStaminaValue = 200.0f; public static float currentStaminaValue; public static float decreasingStaminaValue = 50.0f; public static float recoveringStaminaValue = 70.0f; public static bool isSprinting = false; public static bool canRegen = false;

 // Use this for initialization
 void Start () {
     currentStaminaValue = maxStaminaValue;
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyUp (KeyCode.LeftShift)) {
         print ("is not pressed");
         isSprinting = false;
         canRegen = true;
             }

     if (Input.GetKeyDown (KeyCode.LeftShift)&& currentStaminaValue > 0) {
         print("is pressed");
         isSprinting = true;
         canRegen = false;
             }

     if (isSprinting) {
                     currentStaminaValue -= decreasingStaminaValue * Time.deltaTime;
                     Player.playerSpeed = 15.0f;
             } else {
         Player.playerSpeed = 5.0f;
             }

     if (canRegen) {
         if (currentStaminaValue != maxStaminaValue){
             float regenAmount = Mathf.Clamp (60.0f, 0.0f, 200.0f);
             print("regenAmount is " + regenAmount);
             currentStaminaValue += regenAmount * Time.deltaTime;
         }
             }
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kebabs · Feb 14, 2014 at 10:28 PM

I fixed this myself. Didn't even have to use mathf.clamp just a case of proper if statements using <=0 and < to achieve what I wanted. :)

if (currentStaminaValue < maxStaminaValue){ } instead of if (currentStaminaValue != maxStaminaValue) and if (currentStaminaValue <= 0) { } This gave me the desired results

Comment
Add comment · Share
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

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My player shakes when Mathf.Clamp stops it from going off screen. 1 Answer

Clamp not working. (solved) 1 Answer

Another Mathf.Clamp Not Working on Rotate? 1 Answer

Mathf.Clamp() or your own Clamp fn, which is better? 1 Answer

Make my player who uses Character Controller to stop moving in one certain direction once it reaches the borders. 1 Answer


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