Detecting when the Y axis is rising
I'm basically making a game where you have a spaceship and you fly up into the sky with a height amount on the side. I want this height amount to of course increase when the player gets higher and to decrease when the player gets lower but I'm not sure on how to do it?.
Could anyone give me an idea on how to do it?
Answer by Downstream · Mar 25, 2016 at 08:35 PM
I would suggest you go view the live training or basic tutorials first. This sort of task will be trivial to anyone who's viewed them.
Basically you want a script with an update function that takes the Transform position y value and turns it in to a string in a text field the child of a Canvas object.
Things you need to know:
Scripting (first six beginner tutorials)
UI(Canvas and Text)
Once you've got your pieces laid out (your game object with the script, a Canvas and a Text field) it's this simple:
using UnityEngine;
using UnityEngine.UI;
public class Altimeter : MonoBehaviour{
public Text textFieldComponent; // Just select the text (not GUI text) field from the inspector.
void Update(){
textFieldComponent.text = transform.position.y.ToString("00.00"); // Parameters in the ToString function formats the altitude to two decimal places.
}
}