- Home /
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.
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 >.<
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.
Yeah, could you post more code, I tried to do that before but it wasn't working. Thank you.
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;
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);
}
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
Follow this Question
Related Questions
Spaceship movement 2 Answers
Convert world space to GUI Rect? 5 Answers
Scroll to change walk speed... 1 Answer