add: pistol weapon and enemy

This commit is contained in:
KOSMOGOR
2025-03-08 22:41:34 +03:00
parent 7d6cef4989
commit 17f3448f6c
14 changed files with 542 additions and 3 deletions
+30
View File
@@ -0,0 +1,30 @@
using UnityEngine;
public class BulletProjectile : MonoBehaviour
{
public float speed = 1f;
public float damage = 20f;
public float timeToLive = 5f;
float currentTime = 0;
Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
rb.linearVelocity = transform.forward * speed;
}
void Update() {
currentTime += Time.deltaTime;
if (currentTime >= timeToLive) Destroy(gameObject);
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.CompareTag("Enemy")) {
Enemy enemy = other.GetComponent<Enemy>();
enemy.DealDamage(damage);
}
Destroy(gameObject);
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: e7a451ad0b53e8c0ca82f7ce15114c38
+12
View File
@@ -0,0 +1,12 @@
using UnityEditor.Embree;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float hp = 100f;
public void DealDamage(float damage) {
hp -= damage;
if (hp <= 0) Destroy(gameObject);
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 46894d927b8e6654487b876457ca8974
+21
View File
@@ -0,0 +1,21 @@
using UnityEngine;
public class Pistol : Weapon
{
public GameObject projectilePrefab;
public Transform spawnPosition;
public float fireRate = 1f;
float currentDelay = 0;
public override void Shoot(bool keyDown, bool keyHold) {
if (keyDown && (fireRate == 0 || currentDelay == 1 / fireRate)) {
Instantiate(projectilePrefab, spawnPosition.position, transform.rotation);
currentDelay = 0;
}
}
void Update() {
if (fireRate != 0) currentDelay = Mathf.Clamp(currentDelay += Time.deltaTime, 0, 1 / fireRate);
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9b5e56940d7855caeb2d584265db0624
+13
View File
@@ -0,0 +1,13 @@
using UnityEngine;
public abstract class Weapon : MonoBehaviour
{
public bool isActive { get; private set; } = true;
public abstract void Shoot(bool keyDown, bool keyHold);
public void SetWeaponState(bool active) {
isActive = active;
GetComponent<MeshRenderer>().enabled = active;
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 918a4cc8f96795ab080e3de45b90632c
+19
View File
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using UnityEngine;
public class WeaponController : MonoBehaviour
{
[SerializeField] Weapon[] weapons;
[SerializeField] int currentWeapon = 0;
void Start() {
weapons = GetComponentsInChildren<Weapon>();
for (int i = 0; i < weapons.Length; ++i) {
if (i != currentWeapon) weapons[i].SetWeaponState(false);
}
}
void Update() {
if (Input.GetMouseButton(0) && weapons[currentWeapon].isActive) weapons[currentWeapon].Shoot(Input.GetMouseButtonDown(0), Input.GetMouseButton(0));
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: f3724861d6ef4a112910a3ef14207757