Player takes damage from bullets they shot but not shots enemies shot

so when the player hits the space bar after 2 shots they die but when the enemies shoot a thousand times the player doesn’t die. when I tried printing the amount of damage he should take when the enemies shoot he didn’t even write zero but when he shoots it wrote 100

here’s the player code

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

public class Player : MonoBehaviour
{
    [Header("Player")]
    [SerializeField] float moveSpeed = 10f;
    [SerializeField] float padding = 1f;
    [SerializeField] int health = 200;
    [Header("laser")]
    [SerializeField] GameObject laserprefab;
    [SerializeField] float projectileSpeed = 10f;
    [SerializeField] float projectileFiringPeriod = 0.1f;

    Coroutine firingCoroutine;

    float xMin;
    float xMax;
    float yMin;
    float yMax;

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



    // Update is called once per frame
    void Update()
    {
        Move();
        Fire(); 
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
        ProcessHit(damageDealer);

    }

    private void ProcessHit(DamageDealer damageDealer)
    {
        health -= damageDealer.GetDamage();
        if(health <= 0)
        {
            Destroy(gameObject);
        }
    }

    private void Fire()
    {
        if (Input.GetButtonDown("Fire1"))
        {
          firingCoroutine = StartCoroutine(FireContinuosly());
        }
        if (Input.GetButtonUp("Fire1"))
        {
            StopCoroutine(firingCoroutine);
        }
    }

    IEnumerator FireContinuosly()
    {
        while (true)
        {
            GameObject laser = Instantiate(laserprefab, transform.position, Quaternion.identity) as GameObject;
            laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
            yield return new WaitForSeconds(projectileFiringPeriod);
        }
    }


    private void Move()
    {
        var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
        var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;

        var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);
        var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax);
        transform.position = new Vector2(newXPos, newYPos);
    }
    private void SetUpMoveBoundaries()
    {
        Camera gameCamera = Camera.main;
        xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding;
        xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding;
        yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding;
        yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;

    }

}

heres the Damagedealer script

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

public class DamageDealer : MonoBehaviour
{

    [SerializeField] int damage = 100;

    public int GetDamage()
    {
        return damage;
    }

    public void Hit()
    {
        Destroy(gameObject);
    }

}

Hi Dave,

Welcome to our community! :slight_smile:

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? If so, add a Debug.Log to the player’s collision method to see if the method gets called when an enemy laser collides with the player.

yes the player takes damage from the player laser and I have compared my code to the source code and they are the same to my view

thank you for your fast reply

Log the value of damageDealer.GetDamage(); into your console. If the value is 0, you found the problem. In that case, check the DamageDealer component in the Inspector of the enemy prefab.

Yes I have checked and to my view, it does not recognize the hit when the enemy laser hits the player and I can’t see why because I have made the polygon collider a trigger

Do the enemy lasers have got trigger colliders? And the player has got a non-trigger collider assigned?

so you mean is the trigger is enabled on the laser cause if so then yes

If the player’s lasers can hit the player but the enemy’s lasers can’t, there must be a difference between them. Are there any error messages in your console when the enemy laser hits the player?

Have you already watched the lecture named “Layer Collision Matrix” (currently #107)? If so, check the layer of the enemy laser and the collision matrix whether the enemy laser layer can interact with the player layer.

thanks, it was 2 problems I just found out of that, I had forgotten to add damage dealer to the laser and enemy laser and player were not allowed to collide

sorry for wasting your time

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms