I'm trying to deal damage to a zombie GameObject with a SphereCast that originates from my First Person Camera when the animation from my melee weapon plays.
The problem is that I'm getting a NullReferenceException in the Physics.SphereCast line and I can't figure out why. I've checked every inspector of gameObjects that could be related to this and everything was assigned.
The AiExample Script is attached to every zombie.
public class PlayerAttack : MonoBehaviour
{
private WeaponManager weaponManager;
private PlayerAxeWooshSounds axeWooshSounds;
public float fireRate = 400f;
public float singleFireRate = 1f;
private float NextTimeToFire = 0.0f;
private float lastFired;
public float damage = 20f;
private Animator FPanim;
private GameObject crossHair;
private bool zoomed;
private Camera mainCam;
public int AxeDamage = 25;
public Transform sphereCastSpawn;
private WeaponHandler weaponHandler;
void Start()
{
weaponManager = GetComponent<WeaponManager>();
FPanim = transform.Find(Tags.LOOK_ROOT).transform.Find(Tags.FIRST_PERSON_CAMERA).GetComponent<Animator>();
crossHair = GameObject.FindGameObjectWithTag(Tags.CROSSHAIR);
mainCam = Camera.main;
weaponHandler = GetComponentInChildren<WeaponHandler>();
}
void Update()
{
WeaponShoot();
ZoomInAndOut();
}
void WeaponShoot()
{
if(Input.GetMouseButtonDown(0))
{
if(weaponManager.GetCurrentSelectedWeapon().tag == (Tags.AXE_TAG))
{
weaponManager.GetCurrentSelectedWeapon().ShootAnimation();
RaycastHit hit;
if(Physics.SphereCast(origin: sphereCastSpawn.position, 0.5f, sphereCastSpawn.TransformDirection(Vector3.forward),
out hit, weaponHandler.zombieLayer))
{
hit.transform.GetComponent<AiExample>().DamageZombie(AxeDamage);
}
}
}
}