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 spicypeachdev · Jun 13, 2020 at 06:50 AM · runsprintzerosprintingstamina

How to disable sprinting when slider reaches zero.

So I need some help...

I have my controls so that when the player presses the shift button while moving, the character begins sprinting. I've also managed to have the stamina bar in a way so that the filling gradually goes down when holding shift. My problem is that I'm not sure how to edit my code to where when the stamina bar reaches 0, the player can no longer sprint.

Here's the Stamina Bar code:

{

 public Slider slider;

 public void SetMaxStamina(int stamina)
 {
     slider.maxValue = stamina;
     slider.value = stamina;
 }

 public void SetStamina(int stamina)
 {
     slider.value = stamina;
 }

 public int maxStamina = 200;
 public int currentStamina;
 public StaminaBar staminaBar;

 void Start()
 {
     currentStamina = maxStamina;
     staminaBar.SetMaxStamina(maxStamina);
 }


 void Update()
 {
     if (Input.GetKey(KeyCode.LeftShift))
     {
         TakeDamage(20);
     }
 }

 void TakeDamage(int damage)
 {
     currentStamina -= damage;

     staminaBar.SetStamina(currentStamina);
 }

}

Here's the player movement script:

{ private float jumpSpeed = 5; private Rigidbody rigidBody; private bool onGround = true; private const int MAX_JUMP = 2; private int currentJump = 0;

 public float walkSpeed;
 public float sprintSpeed;

 public GameObject slider;



 // Use this for initialization
 void Start()
 {
     rigidBody = GetComponent<Rigidbody>();

     walkSpeed = 5f;
     sprintSpeed = 10f;


 }

 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown("space") && (onGround || MAX_JUMP > currentJump))
     {
         rigidBody.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
         onGround = false;
         currentJump++;
     }

//This is the part I need help with

     StopSprinting();

//I'm not sure how to properly add "if" statements, if that's what I need to do

     transform.Translate(walkSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, walkSpeed * Input.GetAxis("Vertical") * Time.deltaTime);

     //Sprinting

     if (Input.GetKey(KeyCode.LeftShift))

     {
         walkSpeed = sprintSpeed;
     }
     else

     {
         walkSpeed = walkSpeed;
     }
     if (Input.GetKeyUp(KeyCode.LeftShift))
     {
         walkSpeed = 5f;
     }

     void StopSprinting()
     {

     }

 }

 void OnCollisionEnter(Collision collision)
 {
     onGround = true;
     currentJump = 0;
 }

}

If you need any more information, please me know. Thank you.

Comment
Add comment · Show 1
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
avatar image nt314p · Jun 14, 2020 at 12:45 AM 0
Share

You need to take your void StopSprinting() out of the Update function.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tuinal · Jun 13, 2020 at 08:06 AM

Why not just...

if (Input.GetKey(KeyCode.LeftShift))

to

if (Input.GetKey(KeyCode.LeftShift)&&stamina>0)

Comment
Add comment · Show 1 · 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
avatar image spicypeachdev · Jun 13, 2020 at 10:33 PM 0
Share

I've tried replacing the line of script on both of them, but neither one made a difference, unfortunately. Thank you for trying, though

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

128 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 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 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 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 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 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 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 avatar image avatar image

Related Questions

What's wrong with my sprinting logic? 1 Answer

Sprinting animation 1 Answer

How do I script so my character Jumps farther when Running? 1 Answer

Reducing an integer by X per second 1 Answer

Mid air "sprint" 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