首页 > 解决方案 > 如何将位置从一个对象转换为当前玩家位置?

问题描述

所以我在我的播放器上安装了这个小粒子系统,所以如果他死了,他就会爆炸。但我不能简单地将粒子系统附加到玩家下方,因为如果我摧毁我的玩家,游戏对象的孩子也会被摧毁。如果他死了,动画就会运行,但不是在他现在的位置,所以有什么想法吗?也许在玩家死亡时将位置转换为玩家的当前位置?这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerdeath : MonoBehaviour
{

    public ParticleSystem death_explosion;

    // Start is called before the first frame update
    void Start()
    {

        death_explosion.Stop();
    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "deathcube")
            Destroy(gameObject);
        Debug.Log("collision detected");
        death_explosion.Play();
    }
}

标签: c#unity3dcollision-detectionparticlesparticle-system

解决方案


您可以做的是创建粒子对象的预制件并在脚本中引用它,如下所示:

public ParticleSystem death_explosion_Prefab;

而不是将它作为孩子附加到 Player 上,而是在碰撞时实例化它:

void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "deathcube")
        {
            Debug.Log("collision detected");
            Instantiate(death_explosion_Prefab, gameObject.transform.position, Quaternion.identity);
            Destroy(gameObject);
        }
    }

推荐阅读