qq_37296877 2017-05-25 14:00 采纳率: 61%
浏览 985
已采纳

求帮忙,unity的小问题

using UnityEngine;

public class ShellExplosion : MonoBehaviour
{
public LayerMask m_TankMask; // Used to filter what the explosion affects, this should be set to "Players".
public ParticleSystem m_ExplosionParticles; // Reference to the particles that will play on explosion.
public AudioSource m_ExplosionAudio; // Reference to the audio that will play on explosion.
public float m_MaxDamage = 100f; // The amount of damage done if the explosion is centred on a tank.
public float m_ExplosionForce = 1000f; // The amount of force added to a tank at the centre of the explosion.
public float m_MaxLifeTime = 2f; // The time in seconds before the shell is removed.
public float m_ExplosionRadius = 5f; // The maximum distance away from the explosion tanks can be and are still affected.

private void Start ()
{
    // If it isn't destroyed by then, destroy the shell after it's lifetime.
    Destroy (gameObject, m_MaxLifeTime);
}


private void OnTriggerEnter (Collider other)
{
    // Collect all the colliders in a sphere from the shell's current position to a radius of the explosion radius.
    Collider[] colliders = Physics.OverlapSphere (transform.position, m_ExplosionRadius, m_TankMask);

    // Go through all the colliders...
    for (int i = 0; i < colliders.Length; i++)
    {
        // ... and find their rigidbody.
        Rigidbody targetRigidbody = colliders[i].GetComponent<Rigidbody> ();

        // If they don't have a rigidbody, go on to the next collider.
        if (!targetRigidbody)
            continue;

        // Add an explosion force.
        targetRigidbody.AddExplosionForce (m_ExplosionForce, transform.position, m_ExplosionRadius);

        // Find the TankHealth script associated with the rigidbody.
        TankHealth targetHealth = targetRigidbody.GetComponent<TankHealth> ();

        // If there is no TankHealth script attached to the gameobject, go on to the next collider.
        if (!targetHealth)
            continue;

        // Calculate the amount of damage the target should take based on it's distance from the shell.
        float damage = CalculateDamage (targetRigidbody.position);

        // Deal this damage to the tank.
        targetHealth.TakeDamage (damage);
    }

    // Unparent the particles from the shell.
    m_ExplosionParticles.transform.parent = null;

    // Play the particle system.
    m_ExplosionParticles.Play();

    // Play the explosion sound effect.
    m_ExplosionAudio.Play();

    // Once the particles have finished, destroy the gameobject they are on.
    Destroy (m_ExplosionParticles.gameObject, m_ExplosionParticles.duration);

    // Destroy the shell.
    Destroy (gameObject);
}


private float CalculateDamage (Vector3 targetPosition)
{
    // Create a vector from the shell to the target.
    Vector3 explosionToTarget = targetPosition - transform.position;

    // Calculate the distance from the shell to the target.
    float explosionDistance = explosionToTarget.magnitude;

    // Calculate the proportion of the maximum distance (the explosionRadius) the target is away.
    float relativeDistance = (m_ExplosionRadius - explosionDistance) / m_ExplosionRadius;

    // Calculate damage as this proportion of the maximum possible damage.
    float damage = relativeDistance * m_MaxDamage;

    // Make sure that the minimum damage is always 0.
    damage = Mathf.Max (0f, damage);

    return damage;
}

}

m_ExplosionParticles.transform.parent = null;

这行代码实现了什么功能

  • 写回答

2条回答

  • BigWenZi 2017-05-27 02:15
    关注

    m_ExplosionParticles 让这个物体与父集脱离。 换句话说,让m_ExplosionParticles 置身于世界中。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体