Skip to content

Instantly share code, notes, and snippets.

@didacus
Created November 14, 2021 15:05
Show Gist options
  • Save didacus/866c267762cff92747055a22dc2a9d64 to your computer and use it in GitHub Desktop.
Save didacus/866c267762cff92747055a22dc2a9d64 to your computer and use it in GitHub Desktop.
Unity - Basic bullet controller against enemies
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletController : MonoBehaviour
{
public float speed;
public float lifeTime;
public int damageToGive;
void Update()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime); // Bullet direction
/// Bullet lifetime
lifeTime -= Time.deltaTime;
if (lifeTime <= 0)
{
Destroy(gameObject);
}
///
}
/// Bullet collision
void OnCollisionEnter(Collision other)
{
// Collision against a enemy
if (other.gameObject.tag == "Enemy")
{
other.gameObject.GetComponent<EnemyHealthManager>().DamageEnemy(damageToGive);
Destroy(gameObject); // Destroy bullets
}
}
///
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment