I have a zombie game in Unity. In this game, I kill zombies by shooting at the point I click on the screen with the mouse, but sometimes even though the raycast hits the zombie, the zombie is not affected and when I control it, I get the tag and name values of the object behind the zombie. How can I solve this problem? I am using the following line of code. EnabledLayer selections match the zombie collider layers, there is no problem in that part.
public void FireEvent()
{
if (!fireAuth)
return;
if (GameManager_PTH.Instance.IsGameStart)
{
Vector3 getMousePos = Input.mousePosition;
if (MermiBar.mermiIndex > 0)
{
currentDelay = TriggerDelay;
Ray ray = RaycastCamera.ScreenPointToRay(getMousePos);
if (Physics.Raycast(ray, out RaycastHit hit, MaxDistance, EnabledLayer))
{
Debug.LogError("What did I hit: " + hit.collider.gameObject.name);
if (hit.collider.gameObject.CompareTag(HittableTag))
{
Zombie_PTH zombie = hit.collider.gameObject.GetComponentInParent<Zombie_PTH>();
string name = hit.collider.gameObject.name.Trim();
if (name.Contains("HeadCollider"))
zombie.HitYedim(HitType.Kafa, getMousePos);
else if (name.Contains("BodyCollider"))
zombie.HitYedim(HitType.Govde, getMousePos);
else if (name.Contains("ArmCollider"))
zombie.HitYedim(HitType.Govde, getMousePos);
else if (name.Contains("LegCollider"))
zombie.HitYedim(HitType.Govde, getMousePos);
Instantiate(BloodParticle, hit.point, Quaternion.identity);
}
}
StopAllCoroutines();
StartCoroutine(SightPosition());
MermiBar.Shot();
Debug.DrawRay(ray.origin, ray.direction * MaxDistance, Color.green, 0.25f);
SoundSFX_PTH.Instance.PlaySound(SoundSFX_PTH.ClipModel.GUN, 0, 1, false);
}
else
{
//RELOAD SOUND
SoundSFX_PTH.Instance.PlaySound(SoundSFX_PTH.ClipModel.SOUND_EFFECT, 3, 1, false);
}
}
}
EnabledLayer selections match the zombie collider layers, there is no problem in that part.