- Home /
Raycast Shoot Error
My game is a simple FPS. What I'm trying to do is have it so that each time a raycast shot by the player hits an enemy, their health decreases by a defined amount. but when i shoot the enemy once, it dies i cant fix
This is the weapon script
using System.Collections; using System.Collections.Generic; using UnityEngine; using EZCameraShake; using TMPro;
public class sWeapon : MonoBehaviour { //Recoil [Header("Recoil")] public sRecoil recoil;
 //UI
 [Header("Text Mesh Pro UI Field")]
 public TextMeshProUGUI WeaponAmmoUI;
 //Camera Shake
 [Header("Camera Shake")]
 public CameraShaker CameraShake;
 //Audio
 [Header("Audio Settings")]
 public AudioSource ShootSound;
 public AudioSource ReloadSound;
 [Header("Particle System Settings")]
 //Particle System
 public ParticleSystem ShootMuzzleFlash;
 //Public Variables
 [Header("Weapon Configures")]
 public float damage = 10f;
 public float range = 200f;
 public float FireRate = 15f;
 //Ammo 
 [Header("Ammo Settings")]
 public int maxAmmo = 10;
 //Reload
 [Header("Reaload Settings")]
 public float reloadtime = 1.9f;
 private bool isReloading = false;
 private int CurrentAmmo;
 //Animator
 [Header("Animator")]
 public Animator animator;
 [Header("Camera")]
 public Camera fpsCamera;
 private float nextTimeTofire = 0f;
 void Start()
 {
     animator = gameObject.GetComponent<Animator>();
     CurrentAmmo = maxAmmo;
 }
 private void OnEnable()
 {
     isReloading = false;
     animator.SetBool("Reload", false);
 }
 void Update()
 {
     //UI TEXT
     WeaponAmmoUI.SetText(CurrentAmmo + " / " + maxAmmo);
     if (isReloading)
         return;
     if (CurrentAmmo <= 0)
     {
         StartCoroutine(Reload());
         return;
     }
     if (Input.GetButton("Fire1") && Time.time >= nextTimeTofire)
     {
         nextTimeTofire = Time.time + 1f / FireRate;
         Shoot();
     }
 }
 IEnumerator Reload()
 {
     isReloading = true;
     Debug.Log("Reloading..");
     ReloadSound.Play();
     animator.SetBool("Reload", true);
     yield return new WaitForSeconds(reloadtime);
     animator.SetBool("Reload", false);
     yield return new WaitForSeconds(0.5f);
     CurrentAmmo = maxAmmo;
     isReloading = false;
 }
 void Shoot()
 {
     if (isReloading)
         return;
     //Particles sound recoil// 
     ShootSound.Play();
     ShootMuzzleFlash.Play();
     recoil.Fire();
     //Camera Shake
     //Ammo Reducing
     CurrentAmmo--;
     RaycastHit hit;
     if (Physics.Raycast(fpsCamera.transform.position, fpsCamera.transform.forward, out hit, range))
     {
         Vector3 fpsCamera = transform.TransformDirection(Vector3.forward) * 10;
         Debug.DrawRay(transform.position, fpsCamera, Color.red);
         Debug.Log(hit.transform.name);
         sTarget target = hit.transform.GetComponent<sTarget>();
         if (target != null)
         {
             target.TakeDamage(damage);
         }
     }
 }
}
This is my enemy script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class sTarget : MonoBehaviour { //Public Variables public float Health = 200f;
 public void TakeDamage (float amount)
 {
     Health =- amount;
     if (Health <= 0f)
     {
         Die();
     }
     void Die ()
     {
         Destroy(gameObject);
         Debug.Log("died");
     }
 }
}
Answer by AbandonedCrypt · Mar 12, 2021 at 01:03 PM
You are setting Health to -amount, which is automatically below zero for any positive damage value. what you are trying to do is health -= amount; not health =- amount; 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                