2026-01-09 08:45:22 +03:00

37 lines
1.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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; // ñêîðîñòü, ìîæíî íàñòðîèòü
}
}
}