1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- using System;
- using System.Collections;
- using TMPro;
- using UnityEngine;
- using UnityEngine.PlayerLoop;
- namespace Comp
- {
- public class MissText : MonoBehaviour
- {
- private Transform _targetObject; // 目标物体
- private const float Speed = 0.3f; // 移动速度
- private const float High = 0.7f;
- // 淡出相关
- private TextMeshPro _textMeshPro;
- private const float FadeTime = 1f;
- private float _timePassed = 0f;
- public void SetTarget(Transform transform)
- {
- _targetObject = transform;
- var position = transform.position;
- this.transform.position = new Vector3(position.x, High, position.z);
- }
- private void Start()
- {
- _textMeshPro = GetComponent<TextMeshPro>();
- }
- public void FixedUpdate()
- {
- if (_targetObject != null)
- {
- var position = _targetObject.position;
- var position1 = transform.position;
- transform.Translate(new Vector3(position.x - position1.x,
- Vector3.up.y * (Speed * Time.deltaTime), position.z - position1.z));
- }
- // 计算当前已经过去的时间
- _timePassed += Time.deltaTime;
- // 计算当前的透明度
- float alpha = Mathf.Lerp(1f, 0f, _timePassed / FadeTime);
- _textMeshPro.alpha = alpha;
- // 如果透明度已经为0,就销毁游戏对象
- if (alpha <= 0f)
- {
- Destroy(gameObject);
- }
- }
- }
- }
|