Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DayyanSisson · Aug 17, 2011 at 05:56 PM · guispeedchangespaceacceleration

Changing Speed in Gameplay Affects GUI

I am making a Space Sim. Right now, my ship can boost, increase it's speed, and decrease it's speed. What I need is a GUI of some sort that shows the increase or decrease of speed. I'm not sure how to do that. Here's my speed script:

 var defaultSpeed = 50.0; // speed to default towards if no keys are pressed
 var maxSpeed = 75.0; 
 var minSpeed = 25.0;
 var maxAcceleration = 5.0; // Controls the acceleration
 var currentSpeed = 50.0;
 
 // var Player = transform;  // this line gave me an error and it isn't needed any way
 
 function Update() 
 {
     transform.Translate(Vector3.forward * Time.deltaTime * currentSpeed);
 
     if(Input.GetKey("up"))
     currentSpeed += maxAcceleration * Time.deltaTime;
     else if(Input.GetKey("down"))
     currentSpeed-=maxAcceleration * Time.deltaTime;
     else if (currentSpeed > defaultSpeed){
         currentSpeed -= maxAcceleration * Time.deltaTime;
         currentSpeed = Mathf.Clamp(currentSpeed, defaultSpeed, maxSpeed);
     }
     else {
         currentSpeed += maxAcceleration * Time.deltaTime;
         currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, defaultSpeed);
     }
     currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
 }

The current speed always updates itself in the inspector when I speed up, or slow down, so the current speed is what I want to show. I'm not sure how to show that in GUI, so help would be greatly appreciated. Thanks in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SilverTabby · Aug 17, 2011 at 06:03 PM

A quick search of the Unity Scripting Refrence will find the GUI class. Inside the GUI class, we find two methods that can do what you are asking for: Horizontal Slider and Vertical Slider. All you have to do is feed your current speed value into the scroll bar, and everything should work.

Just don't read the speed coming out of the function other wise the user can use that to accelerate/decelerate.

Also, in order for the GUI to appear, you have to have all GUI calls inside a special method called OnGUI() - It's basically the Update() function for GUIs.

edit: did I say scroll bar? I meant slider >.<

Comment
Add comment · 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
0

Answer by tzr15 · Aug 17, 2011 at 06:12 PM

You can also make a custom graphic like a frame, with a colored bar inside and then set the pixelInset.width of the bar using your current speed as the driver.

I can post more code if that doesn't make sense.

Comment
Add comment · Show 4 · 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 DayyanSisson · Aug 17, 2011 at 06:21 PM 0
Share

Yeah, could you post more code, I tried to do that before but it wasn't working. Thank you.

avatar image tzr15 · Aug 19, 2011 at 02:12 AM 0
Share

ok.. here is what I do...

In my scene, I create a Game Object for my speed bar and call it GreenSprintBar

then in code...

//declare float that will be used to set my speed graphic (I use a percentage, 0 is none, 1 is full) private var greenProgress : float = 1;

//declare actual full width if at full speed (in pixels) private var fullBarWidth : float = 180;

//declare var for my bar texture private var theGreenBar;

//attach the game object to my code theGreenBar = GameObject.Find("GreenSprintBar");

//then simply use the speed variable that is set elsewhere to crop my speed bar theGreenBar.guiTexture.pixelInset.width=fullBarWidth*greenProgress;

avatar image SilverTabby · Aug 19, 2011 at 03:37 AM 0
Share

Another option is just using the built in GUI components that come with Unity:

 public static final var $$anonymous$$IN : float = 0.0;
 public static final var $$anonymous$$AX : float = 1.0;
 
 var screenPos : Rect;
 var currentValue : float = 1.0;
 
 function OnGUI()
 {
     GUI.HorizontalSlider(screenPos, currentValue, $$anonymous$$IN, $$anonymous$$AX);
 }
avatar image DayyanSisson · Aug 19, 2011 at 04:02 AM 0
Share

Okay, I can't try it yet because I didn't have a texture ready, but just in case, is this right?

 private var greenProgress : float = 1;
 private var fullBarWidth : float = 180;
 private var theGreenBar;
 
 function Update () 
 {
     theGreenBar = GameObject.Find("GreenSprintBar");
     theGreenBar.guiTexture.pixelInset.width=fullBarWidth*greenProgress;
 }

I'm going to try to make a texture, but is there a way to use the same script but use GUI. Thanks for all your help 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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Spaceship movement 2 Answers

Convert world space to GUI Rect? 5 Answers

Scroll to change walk speed... 1 Answer

Change color of Gui.Box background to solid black 1 Answer

How to change the text on an other object ? 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