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 TobySk · Sep 25, 2013 at 07:20 PM · movementsprintstamina

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!

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
Best Answer

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.

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 TobySk · Sep 25, 2013 at 07:42 PM 0
Share

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

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

16 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

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


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