- Home /
Enemy Damage
I know this is a duplicated question but the other kinda went dead. Ive been following the berg zerg arcade hack and slash tutorial to help me with my enemy's, but because he's making a hack and slash game his way of killing the enemy is different to mine. In the script below it says to take damage of when close enough to the enemy and if facing the right direction and so on whereas I want it to take damage of my enemy when my bullet prefab has contact with it. Pleas can anyone help me?
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour { public GameObject target; public float attackTimer; public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 0.30f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.GetButtonDown("Fire1")) {
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
}
}
private void Attack() {
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
Debug.Log(direction);
if(distance < 2.5f) {
if(direction > 0) {
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
eh.AddjustCurrentHealth(-10);
}
}
}
}
Answer by cmccown · Oct 10, 2011 at 05:58 PM
Have you tried making your bullet prefab a rigidbody and making your enemies triggers?
You can create a OnTriggerEnter event handler in the bullet prefab, assign your enemies a tag like ,"HostileTarget" or whatever.
Then in the OnTriggerEnter event handler test the Collider's parameter's tag property to determine if it has the tag you're using to identify enemies.
You can pass damage up to an enemy by sending the Collider a message containing the damage using SendMessageUpwards to pass the function and damage parameters to your enemy class.
The script for this would be something along the lines of (keep in $$anonymous$$d, I'm at work with no intellisense, so research this in the docs): function OnTriggerEnter(other : Collider) { if(other.tag == "HostileTarget") { other.Send$$anonymous$$essageUpwards("ApplyDamage"), damageAmount); } }
You'll need a function on your Enemy base class that all enemy types inherit from that looks something like this: ApplyDamage(damage : int){ health -= damage;}
You'll also need a property in your bullet prefab to represent the damage that projectile causes.
Depending on the type of weapon, you might want to follow ApollyonBob's advice below and use raycasting.
"Bullets" or "Lazahs" fly too fast to bother instantiating, but for something like a missile, grenade, slingshot type weapon that moves slow or follows a trajectory that needs to be modified in flight you'll want to create an actual object instance to represent.
There is an error saying unexpected token: ,. Ive tried to fix it but I'm not the best of scripters, could you please help?
Below is an event handler I've used before for missiles striking an enemy object. You'll have to edit the script, but it will be good practice.
function OnTriggerEnter(other : Collider) { if(other.gameObject.tag == "HostileTarget") { other.gameObject.Send$$anonymous$$essageUpwards("ApplyDamage", 5);
Instantiate(explosion, transform.position, transform.rotation); Destroy(this.gameObject); } }
As stated in the previous comment you'll need to implement a function in your enemy objects that handles applying the damage. If you use this script it must be called ApplyDamage and it must take an integer input. I'm still not convinced you should be using this method. If you're looking for a beam or bullet type weapon you should definitely try using a Raycast ins$$anonymous$$d.
So do I add the script in your first comment onto my bullet prefab and the script in your second comment to my enemy?
The event OnTriggerEnter would go in your bullet prefab.
You'll need another script called ApplyDamage that exists in your enemies. Each enemy needs to be market with the tag "HostileTarget".
The amount of damage caused by your weapon would be modified by changing the value passed into ApplyDamage. (currently 5)
Answer by apollyonbob · Oct 10, 2011 at 06:35 PM
To be honest, I don't think a bullet prefab is the best way to go. Especially since if the player can see your bullets, they are moving way too slow.
The best way to detect whether or not someone has shot the enemy is to do a Physics.RayCast and use the collider information to see what was hit. You basically shoot the ray out of the front of your gun, and it'll tell you if you hit an enemy, hit a wall, etc. Then whatever you hit should have a hit reaction - a blood spurt on an enemy, a spark on a wall (not realistic, but hollywood has condition people to expect it, haha) - combined with the flash from the muzzle, it'll look like a gunshot.
You can modify the ray for each shot to do things like bullet drift, etc. (It won't actually arc, but you're moving the end point around. Easy way to cheat.)
Physics.RayCastAll will tell you everything the ray hit, instead of just the first thing. (This might be useful if you want your bullets to penetrate the enemy then go beyond him.)
I realize that this doesn't answer your immediate question, but I think this is the best way to accomplish your goal.
Your answer
Follow this Question
Related Questions
Making a moving gameobject approach and stop at a target (arrival behaviour) 0 Answers
A node in a childnode? 1 Answer
AI using Javascript 1 Answer
Clearing a destination... 1 Answer
Stay Back! 2 Answers