0% found this document useful (0 votes)
10 views2 pages

Bullet

Uploaded by

lashendh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Bullet

Uploaded by

lashendh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using Unity.

VisualScripting;
using UnityEngine;

public class Bullet : MonoBehaviour


{
private Transform target;
public float speed = 20f;

public int damage = 50;


public float explosionRadius = 0f;

public GameObject impactEffect;

public void Seek (Transform _target) {

target = _target;
}
// Start is called before the first frame update
void Start()
{

// Update is called once per frame


void Update()
{
if (target == null) {
Destroy(gameObject);
return;
}

Vector3 dir = target.position - transform.position;


float distanceThisFrame = speed * Time.deltaTime;

if (dir.magnitude <= distanceThisFrame) {


HitTarget();
return;
}

transform.Translate (dir.normalized * distanceThisFrame, Space.World);

void HitTarget () {

GameObject effectIns = (GameObject)Instantiate (impactEffect,


transform.position, transform.rotation);
Destroy(effectIns, 2f);

if (explosionRadius > 0) {
Explode();

} else {
Damage(target);
}

Destroy(gameObject);
}
void Explode ()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position,
explosionRadius);
foreach (Collider2D collider in colliders) {
if (collider.tag == "Enemy") {
Damage(collider.transform);
}
}
}
void Damage (Transform enemy) {

Enemy e = enemy.GetComponent<Enemy>();

if (e != null) {
e.TakeDamage(damage);
}

void OnDrawGizmosSelected () {

Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, explosionRadius);

}
}

You might also like