- Home /
Implementing Damage to Gun, best way to implement health and damage.
Hello. I currently have a gun script working for a top down shooter. Here is the script: using UnityEngine; using System.Collections;
[RequireComponent (typeof (AudioSource))]
public class Gun : MonoBehaviour {
public enum GunType {Semi,Burst,Auto};
public GunType gunType;
public float rpm;
// Components
public Transform spawn;
private LineRenderer tracer;
// System:
private float secondsBetweenShots;
private float nextPossibleShootTime;
void Start() {
secondsBetweenShots = 60/rpm;
if (GetComponent<LineRenderer>()) {
tracer = GetComponent<LineRenderer>();
}
}
public void Shoot() {
if (CanShoot()) {
Ray ray = new Ray(spawn.position,spawn.forward);
RaycastHit hit;
float shotDistance = 20;
if (Physics.Raycast(ray,out hit, shotDistance)) {
shotDistance = hit.distance;
}
nextPossibleShootTime = Time.time + secondsBetweenShots;
audio.Play();
if (tracer) {
StartCoroutine("RenderTracer", ray.direction * shotDistance);
}
}
}
public void ShootContinuous() {
if (gunType == GunType.Auto) {
Shoot ();
}
}
private bool CanShoot() {
bool canShoot = true;
if (Time.time < nextPossibleShootTime) {
canShoot = false;
}
return canShoot;
}
IEnumerator RenderTracer(Vector3 hitPoint) {
tracer.enabled = true;
tracer.SetPosition(0,spawn.position);
tracer.SetPosition(1,spawn.position + hitPoint);
yield return null;
tracer.enabled = false;
}
}
What would be the best way to implement gun damage, and to implement a variable for the enemy to take the damage? Any help would be appreciated.
Answer by KevLoughrey · Apr 30, 2014 at 10:17 PM
It might seem obvious to say so, but generally gun damage should be stored in the gun class and the enemy damage should be stored in the enemy class. There's no reason for the gun to know about or ever interact with the enemy's health, and vice versa. As a result, when the gun or bullets hit the enemy, they should never be able to directly reduce the enemy's health. They should call a method within the enemy class that does that.
I'd create a variable (something like int health = 100;
) in both classes. Then create a method that takes an int as an argument and decrements the health variable by that int's value. For example:
void decrementHealth(int i) {
health -= i;
}
Then to reduce the health of one, you can call decrementHealth(25);
You can make this method a separate script or put it into the parent classes, depending on how you plan on calling it.
So you're saying to put a variable such as the above, in the gun script, and create a enemy health script with the variable health, so it can decrease health from there? Correct?
If possible, could you show a simple example, as certain things I am still trying to learn. Thanks.
Not sure if there's much I can show you, but the enemy health script would look something like:
int health = 100;
void DecrementHealth(int i) {
health -= i;
}
void Update() {
if (health <= 0) {
//Destroy enemy
}
}
Then it really depends on how you handle your collisions. If your bullets are the things checking for the collision, then you can make your enemy call the decrement health method using GameObject.Send$$anonymous$$essage
.