Created
November 14, 2021 15:05
-
-
Save didacus/866c267762cff92747055a22dc2a9d64 to your computer and use it in GitHub Desktop.
Unity - Basic bullet controller against enemies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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