Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Shoot'em up Game! Please help

Discussion in 'Scripting' started by Ralph1388, Aug 14, 2015.

  1. Ralph1388

    Ralph1388

    Joined:
    Jul 25, 2015
    Posts:
    24
    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! :)
    Thanks
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Doesn't the shooter already provide a health system for the player? Just copy that across to the enemy and massage from there.
     
    tedthebug likes this.
  3. EETechnology

    EETechnology

    Joined:
    Aug 15, 2015
    Posts:
    185
    You could put health variable in enemy, and then decrease this variable by the bullet sterngth.
    Enemy.class
    Code (CSharp):
    1. int health = 100;
    2.  
    3. void OnCollisionEnter(Collider bullet){
    4.       if(bullet.gameObject.tag == "Bullet"){
    5.             //Bullet strength is 20
    6.             health -= 20;
    7.       }
    8.       if(health <= 0){
    9.             Destroy(this.gameObject);
    10.       }
    11. }
     
  4. Ralph1388

    Ralph1388

    Joined:
    Jul 25, 2015
    Posts:
    24
    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!
     
  5. EETechnology

    EETechnology

    Joined:
    Aug 15, 2015
    Posts:
    185
    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

    Code (CSharp):
    1. int health = 100;
    2. void OnCollisionEnter(Collider bullet){
    3.       if(bullet.gameObject.tag == "Bullet"){
    4.             //Bullet strength is 20
    5.             health -= 20;
    6.       }
    7.       if(health <= 0){
    8.             Destroy(this.gameObject);
    9.       }
    10. }
     
    Last edited: Aug 15, 2015
  6. Ralph1388

    Ralph1388

    Joined:
    Jul 25, 2015
    Posts:
    24
    Alright, let me try it and see what happens and will get back here and let you know thanks buddy
     
  7. EETechnology

    EETechnology

    Joined:
    Aug 15, 2015
    Posts:
    185
    No problem
     
  8. Ralph1388

    Ralph1388

    Joined:
    Jul 25, 2015
    Posts:
    24
    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<Rigidbody>().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);
    }
    }
     
  9. Ralph1388

    Ralph1388

    Joined:
    Jul 25, 2015
    Posts:
    24
    unfortunely it doesnt, its a simply shot and destroy method
     
    Last edited: Aug 15, 2015
  10. Rokkimies

    Rokkimies

    Joined:
    May 18, 2015
    Posts:
    12
    I think the problem lies in here:
    Code (CSharp):
    1. void OnCollisionEnter(Collider bullet){
    2.       if(bullet.gameObject.tag == "Bullet"){
    3.             //Bullet strength is 20
    4.             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.
    Code (CSharp):
    1. void OnCollisionEnter(Collision bullet){
    2.       if(bullet.collider.tag == "Bullet"){
    3.             //Bullet strength is 20
    4.             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.
     
    Last edited: Aug 16, 2015
  11. Ralph1388

    Ralph1388

    Joined:
    Jul 25, 2015
    Posts:
    24



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


    This is the Health Scripts
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace CompleteProject
    4. {
    5.     public class EnemyHealth : MonoBehaviour
    6.     {
    7.         public int startingHealth = 100;            // The amount of health the enemy starts the game with.
    8.         public int currentHealth;                   // The current health the enemy has.
    9.        
    10.  
    11.         void Awake ()
    12.         {
    13.             // Setting the current health when the enemy first spawns.
    14.             currentHealth = startingHealth;
    15.         }
    16.  
    17.              
    18.         public void TakeDamage (int amount, Vector3 hitPoint)
    19.         {
    20.            
    21.             // Reduce the current health by the amount of damage sustained.
    22.             currentHealth -= amount;
    23.                      
    24.             // If the current health is less than or equal to zero...
    25.             if(currentHealth <= 0)
    26.             {
    27.                
    28.             }
    29.         }  
    30.     }
    31. }
    and this is the damage script
    Code (CSharp):
    1. using UnityEngine;
    2. using UnitySampleAssets.CrossPlatformInput;
    3.  
    4. namespace CompleteProject
    5. {
    6.     public class PlayerShooting : MonoBehaviour
    7.     {
    8.         public int damagePerShot = 20;                  // The damage inflicted by each bullet.
    9.         public float timeBetweenBullets = 0.15f;        // The time between each shot.
    10.        
    11.  
    12.  
    13.         float timer;                                    // A timer to determine when to fire.
    14.         Ray shootRay;                                   // A ray from the gun end forwards.
    15.         RaycastHit shootHit;                            // A raycast hit to get information about what was hit.
    16.         int shootableMask;                              // A layer mask so the raycast only hits things on the shootable layer.
    17.        
    18.  
    19.        
    20.  
    21.         void Update()
    22.         {
    23.             // If the Fire1 button is being press and it's time to fire...
    24.             if (Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0) {
    25.                 // ... shoot the gun.
    26.                 Shoot ();
    27.             }
    28.         }
    29.            
    30.         void Shoot ()
    31.         {
    32.             // Reset the timer.
    33.             timer = 0f;
    34.                        
    35.             // Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
    36.             shootRay.origin = transform.position;
    37.             shootRay.direction = transform.forward;
    38.  
    39.             // Perform the raycast against gameobjects on the shootable layer and if it hits something...
    40.             if(Physics.Raycast (shootRay, out shootHit, shootableMask))
    41.             {
    42.                 // Try and find an EnemyHealth script on the gameobject hit.
    43.                 EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
    44.  
    45.                 // If the EnemyHealth component exist...
    46.                 if(enemyHealth != null)
    47.                 {
    48.                     // ... the enemy should take damage.
    49.                     enemyHealth.TakeDamage (damagePerShot, shootHit.point);
    50.                 }
    51.                                
    52.             }
    53.            
    54.         }
    55.     }
    56. }
    i dont know if they are compatible.. ive spend days on this but no clue!
     
  12. Rokkimies

    Rokkimies

    Joined:
    May 18, 2015
    Posts:
    12
    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.

    Code (CSharp):
    1.  void Awake()
    2. {
    3. shootableMask = LayerMask.GetMask ("Shootable");
    4. }
    Hope this helps =)
     
  13. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.

    http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
     
    Rokkimies likes this.