Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

How do I add stamina correctly?

$
0
0
I am new-ish to JavaScript, and I was using it to add a stamina feature. I am not COMPLETELY new to JavaScript, as I know a bit about it. Can someone help fix my errors? var crchSpeed: float = 3; // crouching speed var runSpeed: float = 20; // run speed private var chMotor: CharacterMotor; private var tr: Transform; private var dist: float; // distance to ground function Start(){ chMotor = GetComponent(CharacterMotor); tr = transform; var ch:CharacterController = GetComponent(CharacterController); dist = ch.height/2; // calculate distance to ground } function Update(){ var vScale = 1.0; var speed = walkSpeed; if (chMotor.grounded && Input.GetKey("left shift") || Input.GetKey("right shift")){ speed = runSpeed; } if (Input.GetKey("c")){ // press C to crouch vScale = 0.5; speed = crchSpeed; // slow down when crouching } chMotor.movement.maxForwardSpeed = speed; // set max speed var ultScale = tr.localScale.y; // crouch/stand up smoothly tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime); tr.position.y += dist * (tr.localScale.y-ultScale); // fix vertical position var stamina = 100; if (speed > 13 && Input.GetKey("left shift")) { var myVars=setInterval(function(){decreaseStamina(x);},1000); if (stamina < 100){ function decreaseStamina(x) = { x -= 5;} decreaseStamina(stamina);}else{console.log("darn");} decreaseStamina(stamina); } {var decreaseStamina = setInterval(function() {decreaseStamina(stamina);}, 2000); if (chMotor.grounded && !Input.GetKey("left shift")) { var myVar=setInterval(function(){coolDown(x)},1000); function coolDown(x) = {if (x < 100) x = x + 5;} coolDown(stamina);} else { console.log("darn"); } } The stamina portion is from line 32 on. From 32 on is completely original. I know this is someone else's code. I am using it to test my JS knowledge. I do not know much about functions, so that is most likely where the problem lies. I get these error codes: Assets/RunAndCrouch.js(37,14): BCE0044: expecting (, found 'decreaseStamina'. and Assets/RunAndCrouch.js(37,39): BCE0044: expecting :, found '-='. If anyone could help, that would be nice.

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>