I’m making a game and there is a button that I want to have a specific float variable appear for a couple of seconds on where the button was clicked and then slide up and disappear.
I don’t know how to do this so any help would be great.
So first you will need to make a text object near or on the button. I assume you know about setting variables with the public types and such. Here’s a sample script that might work
using UnityEngine.UI; // Add this line to the top where all the other "using" commands are
//------------------------
public float FloatToDisplay;
public Text FloatDisplayText;
public GameObject ObjectToLift;
public RectTransform ObjectLiftTransform;
public float TimeToDisplayFor; // Time in seconds to display the float
public Vector3 MovementVariable; // How far it should go in units per second
bool DisplayingText;
//This Is for detecting if it's off the screen
public Camera DisplayCamera;
public float ExtraTime; // How long should the object wait before destroying the script after it's off the screen
public void LiftText() { // Custom Fuction to be called by the button
FloatToDisplay.Text = FloatToDisplay.ToString();
DisplayingText = true;
}
void Update() {
if (DisplayingText == true) {
TimeToDisplayFor += -1 * Time.deltaTime;
}
if (TimeToDisplayFor < 0) {
ObjectToLift.transform.position += MovementVariable * Time.DeltaTime;
}
if (ObjectLiftTransform.transform.position.y > DisplayingCamera.ScreenToWorldPoint(new Vector3(screen.width,screen.height,0).y) {
Destroy(this,ExtraTime);
}
}
You will now need to go into the button, and add a function in the “on click” input field. You then should locate the object that this script is on and select the “Lift Text” custom function. Hopefully that works!
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Test : MonoBehaviour {
public Text text;
private float duration;
private RectTransform rectTransform; //so you can anchor the position
private Vector2 textStartPosition, textEndPosition;
private bool spawned;
float spawnLength; //enter a float for the amount of time text stays visible
void Start() {
rectTransform = text.GetComponent<RectTransform>();
textStartPosition = rectTransform.anchoredPosition;
textEndPosition = new Vector2(0,200);//if your button was at the bottom and you wanted to move up
duration = 4f;
spawnLength = 3f;
}
void Update() {
if (spawned == true){
StartCoroutine(TravelText());
}
else if(spawned == false){
StopCoroutine(TravelText());
}
}
//you would attach the below function to your button or just add the bool to the function you've already created if there's more going on
public void Button(){
spawned = true;
}
IEnumerator TravelText() {
text.enabled = true;
float elapsedTime = 0;
while (elapsedTime < duration) {
float t = elapsedTime / duration;
rectTransform.anchoredPosition = Vector2.Lerp(textStartPosition,textEndPosition,t);
elapsedTime += Time.deltaTime;
yield return new WaitForSeconds(spawnLength);
}
text.enabled = false;
spawned = false;
}
}