40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
using System.Runtime.InteropServices;
|
|
using StarterAssets;
|
|
using UnityEngine;
|
|
|
|
public class Weapon : MonoBehaviour
|
|
{
|
|
[SerializeField] int weaponDamage = 10;
|
|
[SerializeField] ParticleSystem muzzleFlash;
|
|
[SerializeField] GameObject hitVFXPrefab;
|
|
Animator animator;
|
|
StarterAssetsInputs starterAssetsInputs;
|
|
const string SHOOT_STRING = "Shoot";
|
|
void Awake()
|
|
{
|
|
starterAssetsInputs = GetComponentInParent<StarterAssetsInputs>();
|
|
animator = GetComponentInParent<Animator>();
|
|
}
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
HandleShoot();
|
|
}
|
|
void HandleShoot()
|
|
{
|
|
if (!starterAssetsInputs.shoot) return;
|
|
muzzleFlash.Play();
|
|
animator.Play(SHOOT_STRING, 0, 0f);
|
|
starterAssetsInputs.ShootInput(false);
|
|
|
|
RaycastHit hit;
|
|
|
|
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, Mathf.Infinity))
|
|
{
|
|
Instantiate(hitVFXPrefab, hit.point, Quaternion.identity);
|
|
EnemyHealth enemyHealth = hit.transform.GetComponent<EnemyHealth>();
|
|
enemyHealth?.TakeDamage(weaponDamage);
|
|
}
|
|
}
|
|
}
|