You've already forked zumbi-game
54 lines
1.5 KiB
C#
54 lines
1.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class GunController : MonoBehaviour
|
|
{
|
|
[SerializeField] private InputActionReference rightTriggerAction;
|
|
|
|
[Header("Audio")]
|
|
[SerializeField] private AudioSource audioSource;
|
|
[SerializeField] private AudioClip triggerSoundEffect;
|
|
|
|
[Header("Raycast")]
|
|
[SerializeField] private Transform muzzlePoint;
|
|
[SerializeField] private float maxRange = 100f;
|
|
[SerializeField] private float damage = 25f;
|
|
|
|
private void OnEnable()
|
|
{
|
|
rightTriggerAction.action.Enable();
|
|
rightTriggerAction.action.performed += OnTriggerPressed;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
rightTriggerAction.action.performed -= OnTriggerPressed;
|
|
rightTriggerAction.action.Disable();
|
|
}
|
|
|
|
private void OnTriggerPressed(InputAction.CallbackContext context)
|
|
{
|
|
HandleShoot();
|
|
}
|
|
|
|
private void HandleShoot()
|
|
{
|
|
audioSource.PlayOneShot(triggerSoundEffect);
|
|
|
|
Vector3 origin = muzzlePoint != null ? muzzlePoint.position : transform.position;
|
|
Vector3 direction = muzzlePoint != null ? muzzlePoint.forward : transform.forward;
|
|
|
|
if (Physics.Raycast(origin, direction, out RaycastHit hit, maxRange))
|
|
{
|
|
if (hit.collider.CompareTag("Zombie"))
|
|
{
|
|
if (hit.collider.TryGetComponent(out ZombieController zombie))
|
|
{
|
|
zombie.TakeDamage(damage);
|
|
}
|
|
}
|
|
}
|
|
|
|
Debug.DrawRay(origin, direction * maxRange, Color.red, 0.5f);
|
|
}
|
|
} |