- Home /
Movement script with a draining stamina bar
Hello everyone, I have just started out with coding in Java with some help from friends, this forums and other guides. I'm currently making the movement script wich will add a stamina bar to the screen and deplete it as I run (holding shift). It will also make the character unable to run again until currentStamina = maxStamina.... However I rain into a problem with the Collisionflags code with this error:
BCE0051: Operator '&' cannot be used with a left hand side of type 'System.Type' and a right hand side of type 'boolean'.
wich is this line of code:
if(( CollisionFlags & CollisionFlags.CollidedBelow != 0) && !jumping)
{
y = gravity * Time.deltaTime;
}
If it helps, here's the whole code:
var moveDirection : Vector3 = Vector3.zero;
var walkSpeed = 5.0;
var runSpeed = 2.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var tiredSpeed = 0.25;
var curSpeed = 0.00;
var jumping : boolean;
var mayRun : boolean;
// Stamina bar size
var StaminaBarWidth = 0.00;
var StaminaBarHeight = 0.00;
var StaminaPlacementY = 0.00;
// Stamina bar function
var currentStamina = 100.00;
var maxStamina = 100.00;
var curAdjusted = 0;
var allowStaminaReg : boolean = true;
var isRunning : boolean = false;
var _cc = CharacterController;
var _flags = CollisionFlags;
function Start()
{
StaminaBarWidth = Screen.width / 7.30;
StaminaBarHeight = Screen.height / 30.00;
StaminaPlacementY = (20.00);
}
function Update()
{
var x = Input.GetAxis("Horizontal") * curSpeed * Time.deltaTime;
var y = -0.75*Time.deltaTime;
var z = Input.GetAxis("Vertical") * curSpeed * Time.deltaTime;
if(Input.GetKey(KeyCode.LeftShift)&& mayRun)
{
isRunning = true;
SetRunSpeed();
if(curAdjusted != 10.00)
{
curAdjusted = 10.00;
}
if(currentStamina <4)
{
mayRun = false;
}
}
else
{
SetWalkSpeed();
if(curAdjusted != 3.50)
{
curAdjusted = 3.50;
}
isRunning = false;
}
AdjustCurrentStamina(curAdjusted);
if(( CollisionFlags & CollisionFlags.CollidedBelow != 0) && !jumping)
{
y = gravity * Time.deltaTime;
}
moveDirection = new Vector3(x, y, z);
_flags = _cc.Move(moveDirection);
}
function SetWalkSpeed ()
{
if(curSpeed != walkSpeed)
{
curSpeed = walkSpeed;
}
}
function SetRunSpeed()
{
if(curSpeed !=runSpeed)
{
curSpeed = runSpeed;
}
}
function OnGui()
{
GUI.Box (new Rect(5.00, StaminaPlacementY, StaminaBarWidth, StaminaBarHeight), " Stamina");
}
function AdjustCurrentStamina(adjst : float)
{
if (isRunning)
{
currentStamina -= adjst *Time.deltaTime;
}
else
{
currentStamina += adjst*Time.deltaTime;
}
if(currentStamina < 4.00)
currentStamina = 4.00;
if(currentStamina > maxStamina)
mayRun = true;
currentStamina = maxStamina;
if(maxStamina < 1.00)
maxStamina = 1.00;
StaminaBarWidth = (Screen.width / 7.30 ) * (currentStamina / maxStamina);
}
I already now want to say THANK YOU if you take your time with my problem, means alot!
Answer by robertbu · Sep 25, 2013 at 07:26 PM
You have a bit of a mess here. If you had '#pragma strict' at the top of the file, this code would not compile. Your first issue is on line 32:
var _flags = CollisionFlags;
CollisionFlags is a type. But since you are assigning it (and don't have #pragma strict), a variable call CollisionFlags is created of the base type 'System.type'. And _flags then gets that type as well. I think you want to do this:
var _flags : CollisionFlags;
...which sets _flags to the type CollisionFlags.
Further when you do the check you want it to be:
if(( _flags & CollisionFlags.CollidedBelow != 0) && !jumping)
Note since this line comes before the Move() call, you are checking the value from the previous frame.
Thank you very much! That did solve my problem :)
Now I just must get my sta$$anonymous$$a bar to be printed on the GUI and the bar to deplete, but I managed it before so it should not be that big of a problem!
big <3.
Your answer
Follow this Question
Related Questions
Reducing an integer by X per second 1 Answer
Energy Pickup 1 Answer
Why does it give me an error in this small java script for sprint? 1 Answer
How would I make my first person controller sprint, when I hold left shift? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers