I am new to Unity and I just purchased the it a few days ago. I was wondering how to make a Label and how to save it. Also, I was wondering how to tell when a cylinder is not touching the ground (it can’t have Rigidbody Physics) and how to make the number in the label greater when the cylinder is off of the ground.
These may be stupid questions, but I am new to Unity and any help/suggestions would be appreciated! I did look around the internet, but I couldn’t find the answers to these questions anywhere.
No such thing sa stupid questions. Well, these aren’t anyway.
Not sure what you mean by saving a Label… you talkin bout GUI.Label? You could use PlayerPrefs to save the value of whatever GUI.Label is displaying.
You could use the Transform.eulerAngles.y value of your cylinder to determine if it’s above the Transform.eulerAngles.y value of your ground, but the pivot point of your cylinder may not be the bottom of it. I think Unity cylinder primitives have their pivot in their center.
Sorry, but I don’t know what PlayerPrefs is Yes, saving the contents of the label.
I didn’t get that, but say I knew how to see if it was off of the ground, how could I make the number in the label 1 bigger every second or so? In Xcode, I would create an int for that label and just add 1 to the int every second and display the int in the label.
Thank you!
EDIT: Yes, the GUI Text is exactly what I was thinking, thank you
If the ground is flat, then you could tell if the cylinder is above it by using its Y coordinate (transform.position.y). If the Y position of the cylinder is greater than the Y of the ground plus the cylinder’s radius, then the cylinder is in the air. Incrementing the label when the cylinder is above ground is a matter of recording the time when it leaves the ground and displaying the elapsed time since that happened:-
var cylLeaveGroundTime: float;
var cylIsOffGround: boolean;
...
if (cyl.position.y > (groundHeight + cylRadius)) {
if (!cylIsOffGround) {
cylIsOffGround = true;
cylLeaveGroundTime = Time.time;
}
var elapsedTime: float = Mathf.FloorToInt(Time.time - cylLeaveGroundTime);
GUI.Label(labelRect, string.Format("Time: {0}", elapsedTime));
} else {
cylIsOffGround = false;
}
Thanks for responding guys! The surface isn’t flat in all areas, so that won’t work
Let’s make this a little bit more simple. Say I can see if the cylinder is off of the ground or not and I have a Bool that is true if it is off the ground and false if it is touching the ground. What would I put the in if(Bool=true){} statement to make there be a number in a GUI Text and have it grow bigger every time that the Bool is true?
If the ground is not flat, you can measure its distance beneath the object using a raycast down from the object’s centre.
You probably don’t want to count the number of frames in which the test is true, because the frame rate is not consistent. It is a better idea to measure the time as in the above script.
I have one more quick question, how do you make a timer in Unity (Javascript preferably)? I know I can use the function Update method, but that varies on the FPS, is there a way to create a timer that intervals at a controlled rate?
Thank you! I should probably know, but what is the timer preferences and where are they? Also, I am doing a countdown from 60 to 0 and I used
timer=timer-Time.deltaTime;
in my functionUpdate method and it works no matter what the FPS is. Doesn’t the functionUpdate method is the FPS to determine how many times that it is called?
The Time class has a property called fixedDeltaTime that specifies the time between FixedUpdate calls.
You can use Time.deltaTime to get the time since the last frame and thereby make the gameplay time-independent. However, the purpose of FixedUpdate is not to offer a fixed time step as such - its main purpose is for rigidbody operations (adding forces, etc).