- Home /
Problem Draining Energy Float When Sprinting
I'm making a multiplayer game where the players can sprint and I can not get the energy float to go down when sprinting. Players start with 100 energy and I want it to go down by 10 by deltatime. this is my current script. I've looked everywhere and have tried almost everything.
using UnityEngine;
using System.Collections;
public class SprintScript : MonoBehaviour {
private float walkSpeed = 7;
private float sprintSpeed = 13;
private CharacterMotor cMotor;
private CharacterController cController;
//Used in affecting the player's energy.
private PlayerEnergyScript energyScript;
private float energyCost = 10;
// Use this for initialization
void Start () {
if (networkView.isMine == true) {
energyScript = transform.GetComponent<PlayerEnergyScript>();
cMotor = transform.GetComponent<CharacterMotor>();
cController = transform.GetComponent<CharacterController>();
}
else {
enabled = false;
}
}
// Update is called once per frame
void Update () {
float speed = walkSpeed;
if(cController.isGrounded && Input.GetButton("Sprint") && energyScript.energy >= energyCost) {
speed = sprintSpeed;
DrainEnergy();
}
cMotor.movement.maxForwardSpeed = speed;
}
void DrainEnergy () {
energyScript.energy -= energyCost * Time.deltaTime;
}
}
Answer by GimLee · Aug 17, 2014 at 11:21 PM
Maybe my stamina script can inspire you.
public void StaminaDecrement()
{
stamina = stamina + Time.deltaTime * 5;
if (stamina >= 100 && isDead == false)
{
Die();
}
}
This one is starting at stamina = 0 though. And then counting upwards towards 100 where the player will die if it reaches that.
This doesn't really help me much in my current situation but thank you for your comment :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613160833im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I make an energy bar drain from the top down. 3 Answers
Display text when energy reaches 0 2 Answers
Why wont the sprinting on this code work? 1 Answer
Energy refill script bug fix 0 Answers