37 lines
1.0 KiB
C#
37 lines
1.0 KiB
C#
using UnityEngine;
|
||
|
||
public class Gun : MonoBehaviour
|
||
{
|
||
[Header("Ýôôåêòû")]
|
||
public ParticleSystem muzzleFlash;
|
||
|
||
[Header("Îïöèîíàëüíî: ïóëÿ/ëó÷")]
|
||
public GameObject bulletPrefab;
|
||
public Transform barrelEnd;
|
||
|
||
public void Shoot()
|
||
{
|
||
// îáû÷íûé âûñòðåë áåç êîíêðåòíîé öåëè
|
||
if (muzzleFlash != null) muzzleFlash.Play();
|
||
}
|
||
|
||
public void ShootAt(Vector3 targetPos)
|
||
{
|
||
// âêëþ÷àåì ýôôåêòû
|
||
if (muzzleFlash != null) muzzleFlash.Play();
|
||
|
||
// åñëè åñòü ïóëÿ – âûïóñêàåì â íàïðàâëåíèè öåëè
|
||
if (bulletPrefab != null && barrelEnd != null)
|
||
{
|
||
GameObject bullet = Instantiate(bulletPrefab, barrelEnd.position, Quaternion.identity);
|
||
Vector3 dir = (targetPos - barrelEnd.position).normalized;
|
||
bullet.transform.forward = dir;
|
||
|
||
// åñëè ó ïóëè åñòü Rigidbody
|
||
Rigidbody rb = bullet.GetComponent<Rigidbody>();
|
||
if (rb != null)
|
||
rb.velocity = dir * 80; // ñêîðîñòü, ìîæíî íàñòðîèòü
|
||
}
|
||
}
|
||
}
|