using System.Collections.Generic;
using UnityEngine;
namespace Assets.HeroEditor4D.Common.Scripts.ExampleScripts
{
///
/// General behaviour for projectiles: bullets, rockets and other.
///
public class Projectile : MonoBehaviour
{
public List Renderers;
public GameObject Trail;
public GameObject Impact;
public Rigidbody Rigidbody;
public void Start()
{
Destroy(gameObject, 5);
}
public void Update()
{
if (Rigidbody != null && Rigidbody.useGravity)
{
transform.right = Rigidbody.velocity.normalized;
}
}
public void OnTriggerEnter2D(Collider2D other)
{
Bang(other.gameObject);
}
public void OnCollisionEnter2D(Collision2D other)
{
Bang(other.gameObject);
}
private void Bang(GameObject other)
{
ReplaceImpactSound(other);
Impact.SetActive(true);
Destroy(GetComponent());
Destroy(GetComponent());
Destroy(GetComponent());
Destroy(gameObject, 1);
foreach (var ps in Trail.GetComponentsInChildren())
{
ps.Stop();
}
foreach (var tr in Trail.GetComponentsInChildren())
{
tr.enabled = false;
}
}
private void ReplaceImpactSound(GameObject other)
{
var sound = other.GetComponent();
if (sound != null && sound.clip != null)
{
Impact.GetComponent().clip = sound.clip;
}
}
}
}