I’m creating an Enemy AI
How would I make this so that when the bullet hits the enemy, the Health subtracts the amount of damage for the bullet. I’m using Novashot’s gun scripts.
Enemy Script
var Player : Transform;
var MoveSpeed = 4;
var MinDist = 5;
var MaxDist = 10;
var EnemyHealth = 100;
function Start() {
}
function Update() {
transform.LookAt (Player);
if(EnemyHealth == 0){
Destroy(this.gameObject);
}
if(MinDist){
PlayerHealth -- 20;
}
if(Vector3.Distance(transform.position, Player.position) >= MinDist) {
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player.position) <= MaxDist){
}
}
}
You could use OnTriggerEnter: Unity - Scripting API: Collider.OnTriggerEnter(Collider)
Your Enemy would need a Collider with trigger enabled on it and this method in its script. Then whenever another collider enters its trigger zone this function gets called. Instead of just destroying your enemy whenever this happens, you would need to check if the other thing is a bullet and then subtract health. Assign a tag to your bullet to make it easy. All in all, not the best way for a final game, but it gets you started.
Select your bullet prefab and at the top of the inspector there is a field called “Tag”. There you can assign any of the Tags, that are currently configured. Go to Add new… or Project Settings > Tags and Layers to create new tags.
I would reverse things have the bullet contain a script that looks for a health component on what it collides with, that health component would have a TakeDamage(float damage) method.