Search Unity

Health System help PLEASE

Discussion in '2D' started by Robster95, Mar 29, 2020.

  1. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    I am Creating a basic 2D side scrolling game. i have a basic test level along with a gun that has a weapon damage tag that shoots a prefab bullet (not raycasting) that is attached to the player.

    I am not able to make a health system however. I need help making one that uses basic int health that can be carried from one scene to another and when I shoot an enemy the damage dealt is based off of the gun that shoots the bullet.

    I have tried everything that I know of so far and everytime I can only deal damage with a collision of a set amount when the bullet collides with the enemy gameObject. I am not able to carry over the damage of the weapon that shoots it however only a set amount in code. I dont know if this would deal with inheritance or how to go about having the bullet or enemy pull the weapon damage variable from a different script.

    PLEASE if anyone can help me with this i would greatly appreciate it. Ive been struggling on this part of my project.

    P.s. I dont have any scripts to show due to me deleting them after seeing nothing is working.
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    It would all have to do with where you store your damage and where you store your health. If you store the damage in the bullet prefab, make sure you set it correct as you fire, but if you change weapons it won't care - probably the best option. Alternatively save your damage in your gun/player and then read it on collision - will change damage if your gun changes or upgrades.

    Same for health, store it in the enemy prefab if you only care about their health showing there (eg. health bar is part of prefab), otherwise store it somewhere else in like a variable array that both enemies and health objects refer to.

    Let me know if you have any more specific questions
     
  3. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154

    I have the health in its own script but i'm thinking of adding it to the enemy and player prefabs (each have their own health values and everything in their respective code) and i have the damage int in the gun prefabs. the bullet prefab and script is just for it to be able to shoot the bullet gameobject. what my main problem is im not able to shoot damage the enemy due to the weapon damage being in the weapon that holds the damage variable, shoots a bullet that doesnt have a damage variable, and the bullet collides with the enemy. i'm not able to transfer the damage from the weapon to the enemy if that makes sense...
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Well it seems you already know the solution;

    "shoots a bullet that doesnt have a damage variable, and the bullet collides with the enemy"

    So, if you shoot, your weapon will set the damage variable of bullet (which you should add to bullet), sets it velocity and direction if needed.

    When the bullet collides, check if it hit player/enemy and apply bullet damage to player/enemy health component you get from collider.

    "I dont have any scripts to show due to me deleting them after seeing nothing is working."

    Well that is not very constructive. Don't expect anyone to give you full working code. And it is your project... yet it would be easier to show some changes you could make if you had pasted the code here, if it is the code/syntax part you are having problems with.
     
    Last edited: Mar 29, 2020
  5. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    My question is how would i set the damage variable to the bullet? I dont want to have to make multiple prefabs and different bullets to accommodate for all of the different weapons i plan on implementing into the game. Is there a way to carry over the damage from the weapon to the bullet then to the enemy or player for when the bullet collides with them?
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Well - like I already said; have a field for damage in your Bullet script. Attach the script to bullet prefab. When your weapon shoots bullet, create an instance of the bullet, and change its damage value to be damage value of the active weapon. You don't even need to get component, if you store the bullet prefab in a field of type Bullet.

    BTW this question IMO belongs to general scripting forum - there you would get more answers, as most people reading that forum probably don't read 2D graphics forum...
     
  7. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    ok thank you i'll try that. I am newer to game development and learning everything by myself through the manual, tutorials and these forums. It confuses me how i'm able to get the damage variable from one object to another if they dont share the same script
     
  8. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    I don't quite understand what you are saying; It feels like you should learn more C#.

    You do already have your weapon script - right? If you already can shoot bullets, then you must be instantiating those bullets... so it is just something like this:
    Code (CSharp):
    1. public class Weapon : MonoBehaviour
    2. {
    3.     [SerializeField] int weaponDamage;
    4.  
    5.     // Field with type bullet, drop your bullet here
    6.     [SerializeField] Bullet bulletPrefab;
    7.  
    8.     public void Shoot()
    9.     {
    10.         // clone
    11.         var bulletClone = Instantiate(bulletPrefab);
    12.  
    13.         // set damage
    14.         bulletClone.damage = weaponDamage;
    15.  
    16.         // point somewhere
    17.         bulletClone.transform.forward = transform.forward;
    18.     }
    19. }
    20.  
     
  9. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154

    yes i do need to work on my C# skills more. i have a book i am going to be reading soon and i do have my weapon script. i'll look into this more thank you