miepzerino
2025-04-01 8db8d93fb755c7649fd7fd557f34e81b1b0dc7d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using TMPro;
using UnityEngine;
 
public class WorldSpaceText : MonoBehaviour
{
    public Vector3 moveSpeed = new Vector3(0.25f, 0.75f, 0);
    public float oscillationsPerSecond = 1.0f;
    public float timeToFade = 1f;
    public float startToFade = 1f;
    private float timeElapsed = 0f;
    public bool randomStartingPoint = false;
    private float randomStartingPointX = 0f;
    private float randomStartingPointY = 0f;
    private Vector3 startingPosition;
 
    private TextMeshProUGUI textMeshPro;
 
    private void Awake()
    {
        textMeshPro = GetComponent<TextMeshProUGUI>();
        startingPosition = transform.position;
 
        // Add random starting position for text
        if (randomStartingPoint)
        {
            randomStartingPointX = UnityEngine.Random.value - 0.5f;
            randomStartingPointY = UnityEngine.Random.value * 0.15f;
        }
        transform.position = new Vector3(transform.position.x, transform.position.y + randomStartingPointY, transform.position.z);
 
        // Ensure text faces camera
        transform.rotation = Camera.main.transform.rotation;
    }
 
    private void Update()
    {
        timeElapsed += Time.deltaTime;
        float deltaTime = Time.deltaTime;
 
        // Always face camera
        transform.rotation = Camera.main.transform.rotation;
 
        if (timeElapsed >= startToFade)
        {
            // Start fading when "startToFade" time is reached
            textMeshPro.alpha = (1 - ((timeElapsed - startToFade) / timeToFade));
            if (timeElapsed >= (startToFade + timeToFade))
            {
                Destroy(gameObject);
            }
        }
 
        // Calculate oscillation phase
        float phase = ((randomStartingPointX + timeElapsed) * oscillationsPerSecond * (2f * Mathf.PI));
 
        // Set new position of object (using world space values)
        transform.position = new Vector3(
            startingPosition.x + (Mathf.Sin(phase) * moveSpeed.x),
            transform.position.y + (moveSpeed.y * deltaTime),
            transform.position.z + (moveSpeed.z * deltaTime));
    }
}