Tanks Unity Tutorial

来源:互联网 发布:网络诗歌在哪里发布 编辑:程序博客网 时间:2024/05/22 15:39

YouTube地址:https://www.youtube.com/watch?v=rkQmXcQRpTw


Collider . IsTrigger means it doesn't have a solid surface to it can intersect,but it can send a callback to script ,i've now entered another trigger or another collider


1. 给炮弹设置Collider:



Layers : grouping things together in someway that want to use to either include or exclude them from something.


Shell设置:




ShellExplosion.cs 处理碰撞出发以后,只对特定Layer的对象产生影响。Tank 我们设置了 player layer

       Collider[] colliders = Physics.OverlapSphere(transform.position, m_ExplosionRadius, m_TankMask);


using UnityEngine;public class ShellExplosion : MonoBehaviour{    public LayerMask m_TankMask;    public ParticleSystem m_ExplosionParticles;           public AudioSource m_ExplosionAudio;                  public float m_MaxDamage = 100f;                      public float m_ExplosionForce = 1000f;                public float m_MaxLifeTime = 2f;                      public float m_ExplosionRadius = 5f;                  private void Start()    {        Destroy(gameObject, m_MaxLifeTime);    }    private void OnTriggerEnter(Collider other)    {        // Find all the tanks in an area around the shell and damage them.        Collider[] colliders = Physics.OverlapSphere(transform.position, m_ExplosionRadius, m_TankMask);        for (int i = 0; i < colliders.Length; i++  )        {            Rigidbody targetRigidbody = colliders[i].GetComponent<Rigidbody>();            if (!targetRigidbody) {                continue;            }            targetRigidbody.AddExplosionForce(m_ExplosionForce,transform.position,m_ExplosionRadius);            TankHealth targetHealth = targetRigidbody.GetComponent<TankHealth>();            if (!targetHealth)                         continue;            float damage = CalculateDamage(targetRigidbody.transform.position);            targetHealth.TakeDamage(damage);        }        m_ExplosionParticles.transform.parent = null;        m_ExplosionParticles.Play();        m_ExplosionAudio.Play();        Destroy(m_ExplosionParticles.gameObject,m_ExplosionParticles.duration);        Destroy(gameObject);    }    private float CalculateDamage(Vector3 targetPosition)    {        // Calculate the amount of damage a target should take based on it's position.        Vector3 explosionTarget = targetPosition - transform.position;        float explosionDistance = explosionTarget.magnitude;        float relativeDistance = (m_ExplosionRadius - explosionDistance) / m_ExplosionRadius;        float damage = relativeDistance * m_MaxDamage;        damage = Mathf.Max(0f, damage);        return damage;    }}








0 0
原创粉丝点击