Apling damage from a bullet.

Hi all , after all sorry if a make some mistakes on typewriting, iam from spain so, please take me pardon.

Ok i have a turrer with a gameobject on the front of the gun cannon.

In this object i have thease script:

using UnityEngine;
using System.Collections;

public class enemigoataca : MonoBehaviour {
   
    public Transform player;
    public float range = 50.0f; 
    public float bulletImpulse= 70.0f;
    public AudioClip recarga;
    public PlayerInfo character;
    private bool onRange= false;
    public Rigidbody projectile;
    private int damage =50;

    void Start(){
        float rand = Random.Range (1.0f, 2.0f);///TIENPO DE DISPARO
        InvokeRepeating("Shoot", 1, rand);
    }
   
    void Shoot(){
       
        if (onRange){
           
            Rigidbody bullet = (Rigidbody)Instantiate(projectile, transform.position + transform.forward, transform.rotation);
            bullet.AddForce(transform.forward*bulletImpulse, ForceMode.Impulse);
            character.ApplyDamage(damage);
            character.playerInCombat(1);
            AudioSource.PlayClipAtPoint(recarga, transform.position, 1);
            Destroy (bullet.gameObject, 2);

   
        }
       
        }
   
    void Update() {
       
        onRange = Vector3.Distance(transform.position, player.position)<range;
        if (onRange)
        transform.LookAt(player);

    }


   
}

But i dont recieve any damage from him.

This is the script i have on me player where is locatet aall the damage system:

using UnityEngine;
using System.Collections;

public class PlayerInfo : MonoBehaviour {
    public GameObject Bocaarma;
    public GameObject healthBottle;
    public GameObject player;
    public AudioClip deathClip;
    public AudioClip deathClip2;
    public AudioSource sonido;
    public AudioSource sonido2;
    public GameObject sonidoisladino;
    public GameObject sonidoaguadino2;
    public GameObject sonidoaguadino1;
    public GameObject sonidoaguadino3;
    public GameObject sonidoaguadino4;
    public GameObject sonidoaguadino5;
    public GameObject sonidoaguadino6;
    public GameObject sonidoaguadino7;
    public GameObject cave1;
    public Animation muerto2;
    public float length;

    public int Xmin = 400;
    public int Xmax = 700;
    public int Ymin = 300;
    public int Ymax = 800;
    
    int health = 100;
    int damageCount = 0;
    int currentHealth;
    bool playerFighting;
    public GUIText healthbar;
    float timer = 0.0f;
    float healthTimer = 0.0f;
    bool healthShouldRespawn = false;
   
    public bool muerto = false;
    public GameObject tuUI;
   
    void Start () {
        currentHealth = health;
        tuUI.SetActive(false);
        }
   
    void Update () {
        Cursor.visible = false;
        print("player in combat? " + playerFighting);
        if(!playerFighting){
            print("player is not fighting");
            if(currentHealth != 100){
                timer += Time.deltaTime;
                print ("timer: " + timer);
                if(timer > 90.0f){
                    currentHealth += 10;
                    timer = 0.0f;
                }
            }
        }
        else
        {
            if(currentHealth <= 0 && !muerto ){
                muerto = true;
                sonido.PlayOneShot(deathClip2);
                muerto2.Play("muerto");
                GetComponent<FPSInputController>().enabled = false;
                GetComponent<CharacterController>().enabled = false;
                Bocaarma.SetActive(false);
                sonidoisladino.SetActive(false);
                sonidoaguadino2.SetActive(false);
                sonidoaguadino3.SetActive(false);
                sonidoaguadino4.SetActive(false);
                sonidoaguadino5.SetActive(false);
                sonidoaguadino6.SetActive(false);
                sonidoaguadino7.SetActive(false);
                sonidoaguadino1.SetActive(false);
                cave1.GetComponent<AudioSource>().enabled = false;
           
                  


       
            }
        }
        healthbar.text = "Vida " + currentHealth + " / 100";
       
        if(healthShouldRespawn)
        {
            healthTimer += Time.deltaTime;
           
            if(healthTimer > 5.0f)
            {
                healthBottle.gameObject.SetActive(true);
                healthTimer = 0.0f;
            }
        }
       
        if(muerto){
            Cursor.visible = false;
            tuUI.SetActive(true);
            if(Input.GetKeyDown(KeyCode.Space)){
                Respawn();
            }
        }
       
    }
   
    public void ApplyDamage(int damage){
        damageCount = damageCount + damage;
        if(damageCount == 100){
            if(currentHealth < 0){
                currentHealth = 0;
            }
            currentHealth = currentHealth - 30;
            sonido2.PlayOneShot(deathClip);
             
            healthbar.text = "Health " + currentHealth + " / 100";
            damageCount = 0;
        }
       
        print(currentHealth);
    }
   
    public void playerInCombat(int combat){
        if(combat == 1){
            playerFighting = true;
        }
        else
        {
            playerFighting = false;
        }
    }
   
    public void Respawn(){
        tuUI.SetActive(false);
        muerto = false;
        transform.position = new Vector3(Random.Range(Xmin, Xmax),0, Random.Range(Ymin, Ymax));
        GetComponent<FPSInputController>().enabled = true;
        GetComponent<CharacterController>().enabled = true;
        sonidoisladino.SetActive(true);
        sonidoaguadino2.SetActive(true);
        sonidoaguadino3.SetActive(true);
        sonidoaguadino4.SetActive(true);
        sonidoaguadino5.SetActive(true);
        sonidoaguadino6.SetActive(true);
        sonidoaguadino7.SetActive(true);
        sonidoaguadino1.SetActive(true);
        cave1.GetComponent<AudioSource>().enabled = true;
        Bocaarma.SetActive(true);
        currentHealth = 100;
    }
   
    void OnTriggerEnter(Collider item){
        if(item.gameObject.tag == "PlayerHealthBottle")
        {
            if(currentHealth < 100)
            {
                currentHealth = currentHealth + 50;
                item.gameObject.SetActive(false);
                healthShouldRespawn = false;
            }
        }
    }
}

but i cant manage to recieve damage from the bullet i have atached to the script(the first public Rigidbody projectile;)

Or perhaps i neeed a script to atach to the bullet instead?

I beg some healp please.

Thanks in advance.

At the moment your charakter should get damage as soon as he is in range even he isn’t hit by a bullet.

Also could you make a Debug Log right at the first line of your ApplyDamage Method to see if it is even called.

If you want to get the damage from the bullet itself, you can use different ways. One would be to attach colliders on your bullets and geht the OnCollision / OnTriggerEvent or you are looking for collision by yourself with code checking positions

1 Like

Thank you for you answer martinmr, yes the character guets damage even if not hit, thats the big problem, iam looking to make the bullet make me damage instead. but i have barelly no idea how to make it, if i do somthing like this:

using UnityEngine;
using System.Collections;
public class Dañodebala : MonoBehaviour {
public float Damage = 100;

    void OnCollisionEnter
    ( Collision info  ){
    info.transform.SendMessage("ApplyDammage", Damage, SendMessageOptions.DontRequireReceiver);
    }
}

and aply to the bullet, not works at all :frowning:

Ideas pleaseÂż
Thanks

Also some coding suggestions:

public void playerInCombat(int combat){
        if(combat == 1){
            playerFighting = true;
        }
        else
        {
            playerFighting = false;
        }
    }

using

public void playerInCombat(bool combat){
            playerFighting = combat;

    }

and than in your other script

 character.playerInCombat(true);

or false depends on your needs in future

another part:

isn’t it maybe better to first set the charakter in Combat and than adding Damage :slight_smile:

Here maybe first subtracting and than check of current health is below 0 so you don’t get negatvie health.

Also what you can do is
instead of

currentHealth = currentHealth -30;
damageCount = damageCount + damage;

you can do this:

currentHealth -= 30;
damageCount += damage;

To Your bullet problem.

instead of having the Rigidbody

make the projectile a prefab, with a component Rigidbody and a Box Collider 3D or anotger you want to use.

than use public GameObject projectile
also asign a Enemy_Projectile Tag to the projectile prefab like you did to your HealthBottles.

make a projectile class which you also have to asign to your projectile prefab

public class Projectile : MonoBehaviour {
    public int damage = 50;
}

than adding to your player script:

void OnCollisionEnter(Collision collision) {
        if (collision.gameObject.tag == "Enemy_Projectile") {
            ApplyDamage(collision.gameObject.GetComponent<Projectile>().damage);
        }
    }

something like that should. Haven’t it tested so maybe there will be needed some changes.

Also if you have a huge amount of bullets it can take much performance because of rigidbody moving and collider stuff :slight_smile: so you have to check how it will work for you

Thanks , this part i have to remove the
else
{
playerFighting = false;
}
then?, i can erase it?

public void playerInCombat(int combat){
       if(combat == 1){
            playerFighting = true;
       }
       else
       {
            playerFighting = false;
       }
   }

what i changed is;
erase
if(combat == 1){

and

}
else
{
playerFighting = false;
}

so just
playerFighting = true;

remains.

than change to
playerFighting = combat;

and

public void playerInCombat(int combat)

to

public void playerInCombat(bool combat)

so you can do

character.playerInCombat(true);

or

character.playerInCombat(false);
1 Like

sorry for the delay me friend,
so the code stay like this:

public void playerInCombat(bool combat){
        playerFighting = combat;
        playerFighting = true;
        }
        //else
        //{
        //    playerFighting = false;
        //}
    //}

ok i added to me bullet this script:(i made a prefat like you said with rigidbody)

Code (CSharp):

    public class Projectile : MonoBehaviour {
        public int damage = 50;
    }

but where i should place this part in me PlayerInfo script?

    void OnCollisionEnter(Collision collision) {
            if (collision.gameObject.tag == "Enemy_Projectile") {
                ApplyDamage(collision.gameObject.GetComponent<Projectile>().damage);
            }
        }

Sorry for this, i am very nob in this stuff of unity and i need this project for scholl purposes, and i have no many help thanks to you to answer this questions :slight_smile:

this should be your final code. This method Sets your playerFighting the the bool you are giving to the method.

public void playerInCombat(bool combat){
        playerFighting = combat;
       }

Just like your OnTriggerEnter method

wops i have aproblem heuston!!!

i have other enemies like skeletons and mosnters that using this script in general.
After the changes made in the PlayerInfo script i dont recieve damage from him?

Whats wrong now?

code:

using UnityEngine;
using System.Collections;
public class skellyattack6 : MonoBehaviour {
        public GameObject player;
        public GameObject skelly6;
        public float distance;
        public PlayerInfo character;
       
        // Use this for initialization
        void Start () {
        }
       
        // Update is called once per frame
        void Update () {
           
        player = GameObject.FindWithTag("jugador");
        skelly6 = gameObject;
        distance = Vector3.Distance(player.transform.position,skelly6.transform.position);
           
            if (distance <= 4) {
            skelly6.GetComponent<Animation>().CrossFadeQueued("Run", 0.3f, QueueMode.PlayNow);
            skelly6.GetComponent<Animation>().CrossFade("Attack_01");
            character.ApplyDamage(1);
            character.playerInCombat(true);
            }
            else if (distance < 50 && distance > 4.0f) {
            GetComponent<FollowPath>().enabled = false;
            skelly6.GetComponent<Animation>().CrossFadeQueued("Attack_01", 0.3f, QueueMode.PlayNow);
            skelly6.GetComponent<Animation>().CrossFade("Run");
            skelly6.transform.LookAt(player.transform.position);
                transform.position = Vector3.MoveTowards(transform.position, player.transform.position, 0.18f);
                character.playerInCombat(true);
               
            }   
            else
            {
            skelly6.GetComponent<Animation>().CrossFadeQueued("Run", 0.3f, QueueMode.PlayNow);
            skelly6.GetComponent<Animation>().CrossFade("Walk");
            GetComponent<FollowPath>().enabled = true;
            character.playerInCombat(true);
            }
        }
       
       
    }

i have changed the :

character.playerInCombat(true);

instead of

character.playerInCombat(1);

and the rest is like is…

but i dont guet damage now… :frowning:

i have made all the changes you tell me and the final code its like this , may be i do somthing wrong… :frowning:

Code:

using UnityEngine;
using System.Collections;

public class PlayerInfo : MonoBehaviour {
    public GameObject Bocaarma;
    public GameObject healthBottle;
    public GameObject player;
    public AudioClip deathClip;
    public AudioClip deathClip2;
    public AudioSource sonido;
    public AudioSource sonido2;
    public GameObject sonidoisladino;
    public GameObject sonidoaguadino2;
    public GameObject sonidoaguadino1;
    public GameObject sonidoaguadino3;
    public GameObject sonidoaguadino4;
    public GameObject sonidoaguadino5;
    public GameObject sonidoaguadino6;
    public GameObject sonidoaguadino7;
    public GameObject cave1;
    public Animation muerto2;
    public float length;

    public int Xmin = 400;
    public int Xmax = 700;
    public int Ymin = 300;
    public int Ymax = 800;
    //public AnimationClip Die;

    int health = 100;
    int damageCount = 0;
    int currentHealth;
    bool playerFighting;
    public GUIText healthbar;
    float timer = 0.0f;
    float healthTimer = 0.0f;
    bool healthShouldRespawn = false;
   
    public bool muerto = false;
    public GameObject tuUI;
   
    void Start () {
        currentHealth = health;
        tuUI.SetActive(false);
        }
   
    void Update () {
        Cursor.visible = false;
        print("player in combat? " + playerFighting);
        if(!playerFighting){
            print("player is not fighting");
            if(currentHealth != 100){
                timer += Time.deltaTime;
                print ("timer: " + timer);
                if(timer > 90.0f){
                    currentHealth += 10;
                    timer = 0.0f;
                }
            }
        }
        else
        {
            if(currentHealth <= 0 && !muerto ){
                muerto = true;
                sonido.PlayOneShot(deathClip2);
                muerto2.Play("muerto");
                //GetComponent<AudioSource>().enabled = false;
                //GetComponent<AudioSource>().PlayOneShot(deathClip2);//length =5f;
                GetComponent<FPSInputController>().enabled = false;
                GetComponent<CharacterController>().enabled = false;
                Bocaarma.SetActive(false);
                sonidoisladino.SetActive(false);
                sonidoaguadino2.SetActive(false);
                sonidoaguadino3.SetActive(false);
                sonidoaguadino4.SetActive(false);
                sonidoaguadino5.SetActive(false);
                sonidoaguadino6.SetActive(false);
                sonidoaguadino7.SetActive(false);
                sonidoaguadino1.SetActive(false);
                cave1.GetComponent<AudioSource>().enabled = false;
           

               
                


       
            }
        }
        healthbar.text = "Vida " + currentHealth + " / 100";
       
        if(healthShouldRespawn)
        {
            healthTimer += Time.deltaTime;
           
            if(healthTimer > 5.0f)
            {
                healthBottle.gameObject.SetActive(true);
                healthTimer = 0.0f;
            }
        }
       
        if(muerto){                     
           Cursor.visible = false;
            tuUI.SetActive(true);
            if(Input.GetKeyDown(KeyCode.Space)){
                Respawn();
            }
        }
       
    }
   
    public void ApplyDamage(int damage){
            if(damageCount == 100){
            if(currentHealth < 0){
                currentHealth = 0;
            }
            currentHealth -= 30;
            damageCount += damage;
            sonido2.PlayOneShot(deathClip);          
            healthbar.text = "Health " + currentHealth + " / 100";
            damageCount = 0;
        }
       
        print(currentHealth);
    }
   
    public void playerInCombat(bool combat){
        playerFighting = combat;
        playerFighting = true;
    }
    //else
    //{
    //    playerFighting = false;
    //}
    //}

   
    public void Respawn(){
        tuUI.SetActive(false);
        muerto = false;
        transform.position = new Vector3(Random.Range(Xmin, Xmax),0, Random.Range(Ymin, Ymax));
        GetComponent<FPSInputController>().enabled = true;
        GetComponent<CharacterController>().enabled = true;
        sonidoisladino.SetActive(true);
        sonidoaguadino2.SetActive(true);
        sonidoaguadino3.SetActive(true);
        sonidoaguadino4.SetActive(true);
        sonidoaguadino5.SetActive(true);
        sonidoaguadino6.SetActive(true);
        sonidoaguadino7.SetActive(true);
        sonidoaguadino1.SetActive(true);
        cave1.GetComponent<AudioSource>().enabled = true;
         Bocaarma.SetActive(true);
         currentHealth = 100;
    }

    void OnCollisionEnter(Collision collision) {
        if (collision.gameObject.tag == "bala") {
            ApplyDamage(collision.gameObject.GetComponent<Dañodebala>().damage);
        }
    }
   
    void OnTriggerEnter(Collider item){
        if(item.gameObject.tag == "PlayerHealthBottle")
        {
            if(currentHealth < 100)
            {
                currentHealth = currentHealth + 50;
                item.gameObject.SetActive(false);
                healthShouldRespawn = false;
            }
        }
    }
}

Why your are looking for equality ? wouldn be

if(damageCount >=100) better?

you mixed something up in your ApplyDamage Method

should be

    public void ApplyDamage(int damage) {

        damageCount += damage; // Add new DamageCount
        //Than check if  damageCount is higher or equal to your limit 100
        if (damageCount >= 100) {
            //substract 30 Health from your currentHealth
            currentHealth -= 30;
            //check if health is lower than 0 when true it's set to 0 to avoid negative health.
            if (currentHealth < 0) {
                currentHealth = 0;
            }
            sonido2.PlayOneShot(deathClip);
            healthbar.text = "Health " + currentHealth + " / 100";
            damageCount = 0;
        }

        print(currentHealth);
    }

The damageCount has to be 100 or higher in order to take damage? Is “damageCount” a shield or something?

hm yea that could be, also was asking myself why every time to substract 30 health and why not the damage done.

but when the 100 is a shield, it is resetted after it was damage done once

i have 30 damage all the time, i forgot i aplyed the new code you provides and seem to works fine.

Ok , news, the mob before not taken damage before now does…it does 30 damage…

The bullets from the turret.

Makes 30 damage its well…

all makes now 30 damage…

The other problem is the skeletons when in range atack me, follow me and hit me , but when i am out of range before they stop…now its keep going followme all the time …its somthing refent to player in combat, see the general script please.

using 
UnityEngine;
using System.Collections;
public class skellyattack : MonoBehaviour {
public GameObject player;
public GameObject skelly;
public float distance;
public PlayerInfo character;

       
        // Use this for initialization
        void Start () {

    }

       
        // Update is called once per frame
        void Update () {
           
        player = GameObject.FindWithTag("jugador");
        skelly = gameObject;
        distance = Vector3.Distance(player.transform.position,skelly.transform.position);
           
            if (distance <= 3) {
            skelly.GetComponent<Animation>().CrossFadeQueued("Run", 0.3f, QueueMode.PlayNow);
            skelly.GetComponent<Animation>().CrossFade("SwingHeavy");
            character.ApplyDamage(1);
            character.playerInCombat(true);
            }
            else if (distance < 45 && distance > 3.0f) {
            skelly.GetComponent<Animation>().CrossFadeQueued("SwingHeavy", 0.3f, QueueMode.PlayNow);
            skelly.GetComponent<Animation>().CrossFade("Run");
            skelly.transform.LookAt(player.transform.position);
            transform.position = Vector3.MoveTowards(transform.position, player.transform.position, 0.18f);
            character.playerInCombat(true);
               
            }   
            else
            //GetComponent<FollowPath>().enabled = true;
            {
            skelly.GetComponent<Animation>().CrossFadeQueued("Run", 0.3f, QueueMode.PlayNow);
            skelly.GetComponent<Animation>().CrossFade("IdleHoldSword");
            character.playerInCombat(true);
            }
        }
       
       
    }

the bullet has this script on it…it should make 50 damage?

using UnityEngine;
using System.Collections;
public class Dañodebala : MonoBehaviour {
    public int damage = 50;
}

And the skeletons all make 30 …everibody makes 30 damage! o me god! i am a completly nooobbb :frowning:

i try to put :
public int damage =50;

in the top of the skellyatack script but nothing changes its keep make me 30 damage.

if i change it to 1

public int damage =1;

They killme in 1 shot!!

also i try

character.ApplyDamage(damage);

if i change in the Playerinfo script this line:

currentHealth -= damage;

instead of currentHealth -= 30;

and in the skelly atack script

private int damage =10;

and

    character.ApplyDamage(damage);

the skelly make me 10 damage , but very very very fast…in les than 1 second i am death…hope this helps…so close so far…