I have a enemy script and a gun damage script which uses raycasting but my enemy wont take damage and loose health can someone please check whats wrong with this script.
Enemy script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyScript : MonoBehaviour
{
public int EnemyHealth = 10;
void deductPoints(int DamageAmount)
{
EnemyHealth -= DamageAmount;
}
void Update()
{
if (EnemyHealth <= 0)
{
Destroy(gameObject);
}
}
}
Gun damage script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HandGunDamage : MonoBehaviour
{
public int DamageAmount = 5;
public float TargetDistance;
public float AllowedRange = 15.0f;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
RaycastHit Shot;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
{
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange)
{
Shot.transform.SendMessage("DeductPoints", DamageAmount);
}
}
}
}
}
Please help me i have been searching a solution of this for some time now but i cant find a fix for this and i know there was a similiar post like this but i didnt help me. Thanks in advance.
,