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);
}
}