#Bullet not doing damage

1 messages · Page 1 of 1 (latest)

plucky anvil
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShootBullet : MonoBehaviour
{
    public float bulletVelocity;
    public GameObject currentBulletPrefab;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Bullet();
        }

        void Bullet()
        {
            Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            Vector2 direction = (Vector2)((worldMousePos - transform.position));
            direction.Normalize();

            // Creates the bullet locally
            GameObject bullet = (GameObject)Instantiate(
                                    currentBulletPrefab,
                                    transform.position + (Vector3)(direction * 0.5f),
                                    Quaternion.identity);

            // Adds velocity to the bullet
            bullet.GetComponent<Rigidbody2D>().velocity = direction * bulletVelocity;

            StartCoroutine(DestroyBullets()); // Destroys bullets
            IEnumerator DestroyBullets()
            {
                yield return new WaitForSeconds(3);
                Destroy(bullet);

            }
        }
    }
    void OnCollisionEnter(Collision collision)
    {
        if (collision.transform.tag == "Enemy")
        {
            collision.gameObject.GetComponent<Enemy>().TakeDamage(5);
        }
    }
}
#
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class EnemyHealth : MonoBehaviour
{
    public float maxHealth;
    public float currentHealth;
    BoxCollider2D hitbox;

    void Update()
    {
        hitbox = GetComponent<BoxCollider2D>();
    }
    public void TakeDamage(int damageAmount)
    {
        currentHealth -= 5;
        print(currentHealth);

    }
}

#

wait

#

when i hit my Enemy (that has the Enemy layer) it doesnt do anything

winged ravine
#

Check that both collider do not have the trigger option enabled
and that one of them has a rigidbody

plucky anvil
winged ravine
#

rigidbody2d will not work with 3D collider and 3D events

#

you need to use the 2D events for that

#

are you using collider or collider 2D?

plucky anvil
#

box collider 2d for the enemy

plucky anvil
#

sorry i cant figure out what the issue is, im way too tired lol, ill try to check on it tomorrow, thanks for all your help today anyway

plucky anvil
#
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class EnemyHealth : MonoBehaviour
{
    public float maxHealth;
    public float currentHealth;
    Collider2D hitbox;

    void Update()
    {
        hitbox = GetComponent<Collider2D>();
    }
    public void TakeDamage(int damageAmount)
    {
        currentHealth -= 5;
        print(currentHealth);

    }
}
#
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShootBullet : MonoBehaviour
{
    public float bulletVelocity;
    public GameObject currentBulletPrefab;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Bullet();
        }

        void Bullet()
        {
            Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            Vector2 direction = (Vector2)((worldMousePos - transform.position));
            direction.Normalize();

            // Creates the bullet locally
            GameObject bullet = (GameObject)Instantiate(
                                    currentBulletPrefab,
                                    transform.position + (Vector3)(direction * 0.5f),
                                    Quaternion.identity);

            // Adds velocity to the bullet
            bullet.GetComponent<Rigidbody2D>().velocity = direction * bulletVelocity;

           


            StartCoroutine(DestroyBullets()); // Destroys bullets
            IEnumerator DestroyBullets()
            {
                yield return new WaitForSeconds(3);
                Destroy(bullet);

            }
        }
    }
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.transform.tag == "Enemy")
        {
            collision.gameObject.GetComponent<EnemyHealth>().TakeDamage(5);
            Debug.Log("Hit");
        }
    }
}
#

i still cant get it to work, it doesnt print nor make the enemy take damage

zinc citrus
real river
#

The OnCollisionEnter method must be on the bullet

zinc citrus
#
  • Either the bullet or what you're hitting must have a 2D Rigidbody
  • Both the bullet and what you're hitting must have a 2D, non-trigger Collider
plucky anvil
#

Enemy Inspector

#

Bullet Inspector

plucky anvil
zinc citrus
#

Next step: follow Niktu's advice. The bullet itself must have that OnCOllisionEnter code, not whatever fires the bullet

plucky anvil
#

okay, now it knows when the enemy is hit, but im getting this error, from what i understand its something to do with the ```cs
GetComponent<Enemy>()

summer pine
#

Post your Enemy script

#

@plucky anvil

zinc citrus
#

You get the error because class Enemy is not a MonoBehaviour - you sure you have the right class name?

#

No you don't. From the screenshots above you should be getting EnemyHealth instead

summer pine
#

It is recognized as a class, though.

plucky anvil
#

i just fixed the issue but im not sure if my code is efficient
Bullet code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.transform.tag == "Enemy")
        {
            collision.gameObject.GetComponent<Enemy>().TakeDamage(5);
            Debug.Log("Hit");
        }
    }
}
#

Enemy code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public float maxHealth;
    public float currentHealth;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void TakeDamage(int damageAmount)
    {
        currentHealth -= 5;
        print(currentHealth);
        if(currentHealth <=0)
        {
            Destroy(gameObject);
        }

    }
}
summer pine
#

Is the script saved?

plucky anvil
summer pine
#

Good stuff.

plucky anvil
#

I didnt have a Bullet nor Enemy script before, I only had a ShotBullet and EnemyHealth script, I was actually wondering when and what you have to script in to your enemy / player but now I kinda got it

summer pine
#

Alright. You can probably close the thread then. Great success :)

plucky anvil
#

thank you very much for the help everyone ☺️

#

one last thing, when my enemy spawns i get these 3 errors, im not sure what to fix cause when i try to find the problem it just doesnt show in visual studio

using System.Collections;
using UnityEngine;

public class EnemySpawner : MonoBehaviour
{
    public GameObject enemyPrefab;
    public int enemyCount;


    public float minX;
    public float maxX;

    public float minY;
    public float maxY;


    void Start()
    {
        StartCoroutine(SpawnEnemies());
    }

    IEnumerator SpawnEnemies()
    {
        do
        {
            yield return new WaitForSeconds(3);
            Vector2 randomPosition = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
            Instantiate(enemyPrefab, randomPosition, Quaternion.identity);
            enemyCount++;
        }
        while (enemyCount < 10);
    }
}
zinc citrus
#

You probably renamed or deleted a script from your Assets, and now an object that had it is complaining

#

Since the warnings point to the Instantiate call, I would say the missing script is on your enemyPrefab