Shoot'em up Game! Please help

So ive been working on a game or lets say practicing on the Space Shooter game the unity provided. and what i am trying to do is instead of just 1 shot 1 kill enemies i want to put and health and damage system.

If anyone can give me a start and i can play around with it, i would appreciate.
please keep in mind that and a complete beginner so please bare with me! :slight_smile:
Thanks

Doesn’t the shooter already provide a health system for the player? Just copy that across to the enemy and massage from there.

1 Like

You could put health variable in enemy, and then decrease this variable by the bullet sterngth.
Enemy.class

int health = 100;

void OnCollisionEnter(Collider bullet){
      if(bullet.gameObject.tag == "Bullet"){
            //Bullet strength is 20
            health -= 20;
      }
      if(health <= 0){
            Destroy(this.gameObject);
      }
}

Good morning and thank for replying got worried there for a sec. I have the bullet but it just destroys the target instantly i want the bullet to decrease the health… and vise versa for enemies!

Alright, the script above does what you want. When the enemy touches the bullet , enemy’s health is decreased by bulletStrength, in this case 20; so you would need 5 bullets to completely kill the enemy;

Paste this code into your enemy

int health = 100;
void OnCollisionEnter(Collider bullet){
      if(bullet.gameObject.tag == "Bullet"){
            //Bullet strength is 20
            health -= 20;
      }
      if(health <= 0){
            Destroy(this.gameObject);
      }
}

Alright, let me try it and see what happens and will get back here and let you know thanks buddy

No problem

So i tried to play around with it but it keep giving me 2 errors
one the bullet and the other the destroy by contact

Script error: OnCollisionEnter
This message parameter has to be of type: Collision
The message will be ignored.

using UnityEngine;
using System.Collections;

public class Mover : MonoBehaviour
{
public float speed;

void Start ()
{
GetComponent().velocity = transform.forward * speed;
}

int health = 100;

void OnCollisionEnter(Collider bolt)
{
if(bolt.gameObject.tag == “Bolt”)
{
//Bolt strength is 20
health -= 20;
}

if(health <= 0)
{
Destroy(this.gameObject);
}
}
}

and the destory by contact

using UnityEngine;
using System.Collections;

public class DestroyByContact : MonoBehaviour
{
public GameObject explosion;

void Start ()
{

}

int health = 100;
void OnCollisionEnter(Collider bolt)
{
if(bolt.gameObject.tag == “bolt”)
{
//Bolt strength is 20
health -= 20;
}
if(health <= 0)
{
Destroy(this.gameObject);
}
}

void OnTriggerEnter(Collider other)
{
if (other.tag == “Boundary”)
{
return;
}
Instantiate(explosion, transform.position, transform.rotation);
Destroy(other.gameObject);
Destroy(gameObject);
}
}

unfortunely it doesnt, its a simply shot and destroy method

I think the problem lies in here:

void OnCollisionEnter(Collider bullet){
      if(bullet.gameObject.tag == "Bullet"){
            //Bullet strength is 20
            health -= 20;

Now it’s looking for a Collider, when it should look for Collision.

I think this small change should do the trick. Now we check (as we should) the Collision event and get the tag of the entering collider in that event.

void OnCollisionEnter(Collision bullet){
      if(bullet.collider.tag == "Bullet"){
            //Bullet strength is 20
            health -= 20;

My weblaptop can’t run Unity so I can’t test it for you. Scripting documentation is quite helpful if you want to go further down that road.

Ok well i tried to play around with other scripts form other sample projects but its still not working

This is the Health Scripts

using UnityEngine;

namespace CompleteProject
{
    public class EnemyHealth : MonoBehaviour
    {
        public int startingHealth = 100;            // The amount of health the enemy starts the game with.
        public int currentHealth;                   // The current health the enemy has.
       

        void Awake ()
        {
            // Setting the current health when the enemy first spawns.
            currentHealth = startingHealth;
        }

              
        public void TakeDamage (int amount, Vector3 hitPoint)
        {
           
            // Reduce the current health by the amount of damage sustained.
            currentHealth -= amount;
                     
            // If the current health is less than or equal to zero...
            if(currentHealth <= 0)
            {
               
            }
        }  
    }
}

and this is the damage script

using UnityEngine;
using UnitySampleAssets.CrossPlatformInput;

namespace CompleteProject
{
    public class PlayerShooting : MonoBehaviour
    {
        public int damagePerShot = 20;                  // The damage inflicted by each bullet.
        public float timeBetweenBullets = 0.15f;        // The time between each shot.
       


        float timer;                                    // A timer to determine when to fire.
        Ray shootRay;                                   // A ray from the gun end forwards.
        RaycastHit shootHit;                            // A raycast hit to get information about what was hit.
        int shootableMask;                              // A layer mask so the raycast only hits things on the shootable layer.
       

       

        void Update()
        {
            // If the Fire1 button is being press and it's time to fire...
            if (Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0) {
                // ... shoot the gun.
                Shoot ();
            }
        }
           
        void Shoot ()
        {
            // Reset the timer.
            timer = 0f;
                       
            // Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
            shootRay.origin = transform.position;
            shootRay.direction = transform.forward;

            // Perform the raycast against gameobjects on the shootable layer and if it hits something...
            if(Physics.Raycast (shootRay, out shootHit, shootableMask))
            {
                // Try and find an EnemyHealth script on the gameobject hit.
                EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();

                // If the EnemyHealth component exist...
                if(enemyHealth != null)
                {
                    // ... the enemy should take damage.
                    enemyHealth.TakeDamage (damagePerShot, shootHit.point);
                }
                               
            }
           
        }
    }
}

i dont know if they are compatible… ive spend days on this but no clue!

Some things you could try checking out:

File names: EnemyHealth and PlayersShooting should have same file name as their class names. Probably not the problem, if you’re not getting any errors.

Layer masks: in PlayerShooting there’s raycast on shootableMask layer, which is a number what layer the game is checking for the raycast. Problem is, that the shootableMask is not actually set to anything in that script. So first you need to have a layer for objects you want to be able to shoot. You can create a new layer named “Shootable” in Unity and set your enemies to be in that layer. Then from the PlayerShooting script set the shootableMask to be the same number, as is your “Shootable” mask in Unity inspector. This way the raycast knows to check if it hits to a object on that Shootable layer.

Two ways to set: set the int shootableMask be the same as layer mask number in Unity.

And even better way (won’t break if you mess around with layers later)
Put this into PlayerShooting script, it will find layer named “Shootable” and set the shootableMask value to right one.

 void Awake()
{
shootableMask = LayerMask.GetMask ("Shootable");
}

Hope this helps =)

you can’t use a layermask without providing a distance, they are both optional parameters, they are both numeric values. If you only provide one thinking it’s the mask, the int bitmask will be implicitly converted to a float and used as a distance as it’s the 3rd parameter passed into the function.

1 Like