Files
MetalGear2D.Unity/Assets/Scripts/Items/Pistol.cs
T
2025-05-13 05:19:58 +03:00

80 lines
2.2 KiB
C#

using UnityEngine;
using System.Collections;
public class Pistol : PickableItem {
public AudioSource audioOutput;
public RandomSoundPlayer shootSound;
public AudioClip reloadSound;
public Transform shootTransform;
public Animator flash;
private bool flashVisible;
public float shotMaxDistance = 150;
public float singleShotPeriod = 0.5f;
private float _lastShotTime;
public LayerMask shootMask;
public float hitForce = 1000;
public float hitPoints = 15;
public override void OnBeginUse() {
base.OnBeginUse();
if (Time.time - _lastShotTime >= singleShotPeriod) {
Shoot();
_lastShotTime = Time.time;
}
}
// Use this for initialization
protected override void Start () {
base.Start();
// flash.SetActive(false);
}
// Update is called once per frame
void Update () {
}
public void Shoot() {
if (shootSound != null) {
shootSound.Play(true);
}
if (flash != null) {
flash.SetTrigger("Shot");
}
Vector3 right = transform.localToWorldMatrix.MultiplyVector(Vector3.right).normalized;
var hit = Physics2D.Raycast(shootTransform.position, right, shotMaxDistance, shootMask);
if (hit != null) {
//apply force
if (hit.rigidbody != null) {
Debug.Log("Add shoot force to object " + hit.transform.name);
hit.rigidbody.AddForceAtPosition(right * hitForce, hit.point);
}
//apply hit points
if (hit.transform != null) {
var health = hit.transform.gameObject.GetComponent<Health>();
if (health != null) {
health.ApplyDamage(hitPoints);
}
}
}
flashVisible = true;
}
void OnDrawGizmos() {
// if (equipped) {
if (shootTransform != null) {
Vector3 right = transform.localToWorldMatrix.MultiplyVector(Vector3.right).normalized;
Gizmos.DrawLine(shootTransform.position, shootTransform.position + right * shotMaxDistance);
}
// }
}
}