我已将以下脚本附加到名为“ floatingText”的游戏对象上
这有效,但是我只想在对象被破坏时才激活浮动文本。 我在球体/项目符号上附加了另一个销毁脚本,该脚本是通过按屏幕上的按钮以销毁圆柱体对象而触发的。
我的问题是如何使浮动文本仅在撞击位置的子弹破坏了圆柱体时显示。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Float : MonoBehaviour
{
Text text;
public float fadeDuration = 2.0f;
public float speed = 2.0f;
// Use this for initialization
void Start ()
{
text = this.GetComponent<Text>();
StartCoroutine(Fade());
}
public IEnumerator Fade ()
{
float fadespeed = (float)1.0 / fadeDuration;
Color c = text.color;
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime * fadespeed)
{
c.a = Mathf.Lerp(1, 0, t);
text.color = c;
yield return true;
}
Destroy(this.gameObject);
}
// Update is called once per frame
void Update ()
{
this.transform.Translate(Vector3.up * Time.deltaTime * speed);
}
}
控制销毁对象的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Destroy : MonoBehaviour
{
public ParticleSystem effect;
public AudioSource explosion;
public Text scoreText;
public GameObject floatingText;
void OnCollisionExit(Collision col)
{
if (col.gameObject.tag == "Cylinder")
{
effect.Play();
explosion.Play();
Destroy(col.gameObject);
}
}
}
答案 0 :(得分:2)
只需禁用Start
中的对象,以后再启用它:
public class Float : MonoBehaviour
{
Text text;
public float fadeDuration = 2.0f;
public float speed = 2.0f;
// Use this for initialization
void Start ()
{
text = this.GetComponent<Text>();
// Hide object
gameObject.SetActive(false);
}
public ShowTextAndFade(Vector3 initialPosition)
{
StartCoroutine (Fade(initialPosition));
}
private IEnumerator Fade (Vector3 initialPosition)
{
// Show object
gameObject.SetActive(true);
// Set position
transform.position = initialPosition;
float fadespeed = (float)1.0 / fadeDuration;
Color c = text.color;
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime * fadespeed)
{
c.a = Mathf.Lerp(1, 0, t);
text.color = c;
yield return true;
}
Destroy(this.gameObject);
}
// Update is called once per frame
void Update ()
{
this.transform.Translate(Vector3.up * Time.deltaTime * speed);
}
}
,然后在Destroy
脚本中调用它。我假设您想重复使用它,所以我将使用预制件:
floatPrefab
组件的Destroy
中创建的预制件在Destroy
中:
// Reference the prefab in the inspector
public Float floatPrefab;
//...
effect.Play();
explosion.Play();
var position = col.transform.position;
Destroy(col.gameObject);
// Spawn the float text
var float = Instantiate(floatPrefab);
// And start fading
float.ShowTextAndFade(position);
您也可以跳过预制件
删除Instantiate
,而是使用
public Float float;
//...
float.ShowTextAndFade(position);
答案 1 :(得分:1)
应将浮动文本制作为预制文本,以便在使用OnDestroy
事件破坏圆柱体时可以将其实例化。实例化后,您可以通过floatingText
GameObject将信息从圆柱体传递到实例化的浮动文本。
...
public string textToShow = "Floating text you want to show";
public Vector3 pointOfCollision = gameObject.transform.position; // set on impact
void OnDestroy()
{
GameObject floatingText = Instantiate(floatingTextPrefab, pointOfCollision, floatingTextPrefab.transform.localRotation);
floatingText.Text.text = textToShow;
StartCoroutine(floatingText.GetComponent<Float>().Fade());
}