首页 > 解决方案 > 销毁时如何将浮动文本附加到游戏对象

问题描述

我已将以下脚本附加到名为“floatingText”的游戏对象

这可行,但我只想在对象被破坏时激活浮动文本。我有另一个 Destroy 脚本附加到一个球体/子弹,通过按下屏幕上的按钮来销毁 Cylinder 对象来触发该脚本。

我的问题是如何让浮动文本仅在气缸在撞击位置被子弹摧毁时才显示。

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);  
        }
    }
}

标签: c#unity3d

解决方案


只需禁用对象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); 
    

推荐阅读