The code i currently have is
using UnityEngine;
public class Target : MonoBehaviour
{
public float EHealth = 50f;
public void TakeDamage(float amount)
{
EHealth -= amount;
if (EHealth <= 0f)
{
Destroy(gameObject);
}
}
}
for the enemy
and
using UnityEngine;
public class Shooting : MonoBehaviour
{
public float Damage = 10f;
public float Range = 100f;
public Camera fpsCam;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, Range))
{
Debug.Log(hit.transform.name);
Target target = GetComponent();
if (target != null)
{
target.TakeDamage(Damage);
}
}
}
}
for my gun
↧