在特定位置实例化分数闪烁

时间:2016-08-22 13:54:40

标签: c# unity3d

我现在面临一个问题,我希望你能帮助我解决它。我有一个太空射击游戏,一旦你摧毁gameobject,我就会加入得分闪光。我想纠正的是我不知道如何在gameobject被破坏的地方产生得分闪光。它总是在我将它设置为spawn的地方产卵,但我希望它总是在gameobject被破坏的地方产卵。 这是代码:

public GameObject explosion, spawnItem;
public GameObject playerExplosion;
private Done_GameController gameController;
private PlayerHealth playerhealth;
public Text ScoreFlash;
public GameObject canvasg;
public int scoreValue;
public float lifetime;

void OnTriggerEnter (Collider other)
{
    if (other.gameObject.tag == "Boundary" || other.gameObject.tag == "Enemy")
    {
        return;
    }
    if (other.gameObject.tag == "Bullet") 
    {
        playerhealth.AddScore (scoreValue);
        Destroy (gameObject);
        scoreflash ();
    }
    if (other.gameObject.tag == "Player" || other.gameObject.tag == "Bullet") 
    {
        Instantiate (explosion, transform.position, transform.rotation);
        Destroy (gameObject);
    }
    if (CheckIfAsteroid (transform.tag, 3) == "Ast" && other.gameObject.tag == "Bullet") 
    {
        Instantiate (spawnItem, transform.position, Quaternion.identity);
    }
}
string CheckIfAsteroid(string tag, int index)
{
    Debug.Log (tag.Substring (0, Mathf.Min (tag.Length, index)));
    return tag.Substring (0, Mathf.Min (tag.Length, index));
}
    void scoreflash()
{
    ScoreFlash.text = " +" + scoreValue + "p";
    Text newtext = Instantiate(ScoreFlash) as Text;
    GameObject newcanvas = Instantiate (canvasg) as GameObject;
    newtext.transform.SetParent (newcanvas.transform,false);
    Destroy (newcanvas,lifetime);
}

因此scoreflash函数是实例化具有所需值的文本的函数,而字符串检查是否小行星部分用于实例化游戏对象。游戏对象将在那里实例化,文本需要出现。我删除了部分漏洞代码,因为它并不重要。请帮帮我!

2 个答案:

答案 0 :(得分:1)

  

我不知道如何在那里产生得分闪光   游戏对象被摧毁

在销毁之前获取该GameObject的位置,然后调用scoreflash函数并将该位置传入其中。

使用下面的新功能替换OnTriggerEnterscoreflash功能。您可能需要对其进行修改才能正常工作,但这应该可以让您入门。

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Boundary" || other.gameObject.tag == "Enemy")
    {
        return;
    }
    if (other.gameObject.tag == "Bullet")
    {
        playerhealth.AddScore(scoreValue);

        //Get position before destryoing gameobject
        Vector3 diedPos = gameObject.transform.position;
        Destroy(gameObject);
        scoreflash(diedPos);
    }
    if (other.gameObject.tag == "Player" || other.gameObject.tag == "Bullet")
    {
        Instantiate(explosion, transform.position, transform.rotation);
        Destroy(gameObject);
    }
    if (CheckIfAsteroid(transform.tag, 3) == "Ast" && other.gameObject.tag == "Bullet")
    {
        Instantiate(spawnItem, transform.position, Quaternion.identity);
    }
}

void scoreflash(Vector3 diedPosition)
{
    ScoreFlash.text = " +" + scoreValue + "p";
    Text newtext = Instantiate(ScoreFlash) as Text;
    GameObject newcanvas = Instantiate(canvasg) as GameObject;
    newtext.transform.SetParent(newcanvas.transform, false);

    Vector3 screenPos = Camera.main.WorldToScreenPoint(diedPosition);
    newtext.transform.position = screenPos;
    Destroy(newcanvas, lifetime);
}

答案 1 :(得分:0)

你需要获得碰撞的位置并将得分闪变的变换设置为。

我很抱歉我目前无法访问Unity以获取您需要的特定语法,但那是您的问题。你没有告诉Unity应该放置这个新对象的位置,所以如果我不得不猜测它出现在0.0.0,因为那是一个非常正常的默认'。