Question by
deqnboom · Jan 24, 2021 at 02:43 PM ·
damagegun scripthealth
Basic damage script is not working
So I have a Gun script:
using UnityEngine;
public class Gun : MonoBehaviour
{
public int damage = 3;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
// public ParticleSystem bulletEffect;
public GameObject impactEffect;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
muzzleFlash.Play();
//ulletEffect.Play(); //old bullet system
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit))
{
Debug.Log(hit.transform.name);
PlayerHealth playerHealth = hit.transform.GetComponent<PlayerHealth>();
if (playerHealth != null)
{
playerHealth.TakeDamage(damage);
}
}
Transform spawnHit = transform.Find("Cammeras/normal Camera"); //new bullet system
GameObject impact = Instantiate(impactEffect, hit.point + hit.normal, Quaternion.identity) as GameObject;
impactEffect.transform.LookAt(hit.point + hit.normal);
Destroy(impactEffect, 5f);
}
}
and a PlayerHealth script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
public float health = 10f ;
public void TakeDamage(float damage)
{
health -= damage;
Debug.Log("Health = " + health.ToString());
if (health <= 0f)
{
Die();
}
void Die()
{
Destroy(gameObject);
}
}
}
And for some reason the gun can't give any damage to the player. Please I will be really glad if someone can help me.
Comment