weapon system done
This commit is contained in:
1 parent
1af59b921f
commit
f07d687be8
20 files changed
+1504
-52
No files matched your search
@@ -0,0 +1,118 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
// 彈道線 : 每發子彈從槍的位置拉一條線到命中點,很快淡出,只是提示打到哪裡
|
||||
// 掛在 Main Camera 上 (或任何物件),接上 WeaponLoadout 即可
|
||||
public class BulletTracer : MonoBehaviour
|
||||
{
|
||||
[Header("參照")]
|
||||
public WeaponLoadout loadout;
|
||||
|
||||
[Tooltip("Main Camera,線的起點是相對於它的位置")]
|
||||
public Camera aimCamera;
|
||||
|
||||
[Header("外觀")]
|
||||
[Tooltip("起點 (相對於相機) : 往右、往下、往前,大約是槍的位置")]
|
||||
public Vector3 startOffset = new Vector3(0.25f, -0.15f, 0.5f);
|
||||
|
||||
public float width = 0.01f;
|
||||
public Color color = new Color(1f, 0.9f, 0.6f, 0.8f);
|
||||
|
||||
[Tooltip("線停留的時間 (秒)")]
|
||||
public float lifetime = 0.06f;
|
||||
|
||||
[Tooltip("線用的材質,不填會自動用不受光照影響的預設材質")]
|
||||
public Material lineMaterial;
|
||||
|
||||
private class Tracer
|
||||
{
|
||||
public LineRenderer line;
|
||||
public float timeLeft;
|
||||
}
|
||||
|
||||
private readonly List<Tracer> tracers = new List<Tracer>();
|
||||
private HitscanWeapon subscribed;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (lineMaterial == null)
|
||||
{
|
||||
lineMaterial = new Material(Shader.Find("Sprites/Default"));
|
||||
}
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
Subscribe(null);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// 切槍時改聽新的那一把
|
||||
HitscanWeapon current = loadout != null ? loadout.Current : null;
|
||||
if (current != subscribed) Subscribe(current);
|
||||
|
||||
// 淡出並回收
|
||||
foreach (Tracer t in tracers)
|
||||
{
|
||||
if (t.timeLeft <= 0f) continue;
|
||||
|
||||
t.timeLeft -= Time.deltaTime;
|
||||
if (t.timeLeft <= 0f)
|
||||
{
|
||||
t.line.enabled = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
Color c = color;
|
||||
c.a *= t.timeLeft / lifetime;
|
||||
t.line.startColor = c;
|
||||
t.line.endColor = c;
|
||||
}
|
||||
}
|
||||
|
||||
void Subscribe(HitscanWeapon weapon)
|
||||
{
|
||||
if (subscribed != null) subscribed.ShotTraced -= OnShotTraced;
|
||||
subscribed = weapon;
|
||||
if (subscribed != null) subscribed.ShotTraced += OnShotTraced;
|
||||
}
|
||||
|
||||
void OnShotTraced(Vector3 end)
|
||||
{
|
||||
if (aimCamera == null) return;
|
||||
|
||||
Tracer t = GetFreeTracer();
|
||||
t.line.SetPosition(0, aimCamera.transform.TransformPoint(startOffset));
|
||||
t.line.SetPosition(1, end);
|
||||
t.line.startColor = color;
|
||||
t.line.endColor = color;
|
||||
t.line.enabled = true;
|
||||
t.timeLeft = lifetime;
|
||||
}
|
||||
|
||||
// 重複使用已經淡出的線,不夠才新增 (Uzi 連射時同時會有好幾條)
|
||||
Tracer GetFreeTracer()
|
||||
{
|
||||
foreach (Tracer t in tracers)
|
||||
{
|
||||
if (t.timeLeft <= 0f) return t;
|
||||
}
|
||||
|
||||
GameObject go = new GameObject("Tracer");
|
||||
go.transform.SetParent(transform, false);
|
||||
|
||||
LineRenderer line = go.AddComponent<LineRenderer>();
|
||||
line.useWorldSpace = true;
|
||||
line.positionCount = 2;
|
||||
line.startWidth = width;
|
||||
line.endWidth = width;
|
||||
line.material = lineMaterial;
|
||||
line.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
||||
line.receiveShadows = false;
|
||||
|
||||
Tracer created = new Tracer { line = line };
|
||||
tracers.Add(created);
|
||||
return created;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 231fcbcedc598684ea840cde50969a91
|
||||
@@ -26,10 +26,12 @@ public class HitscanWeapon : MonoBehaviour
|
||||
// 給 HUD / 之後的記錄器用
|
||||
public int Ammo { get; private set; }
|
||||
public bool IsReloading => reloadEndTime > 0f;
|
||||
public float ReloadRemaining => IsReloading ? Mathf.Max(0f, reloadEndTime - Time.time) : 0f; // 換彈剩餘秒數
|
||||
public int RecoilSeed { get; private set; } // 後座力隨機部分的種子,記錄下來就能重現
|
||||
|
||||
public event Action Fired;
|
||||
public event Action<BodyPart, float> HitLanded; // 部位、實際傷害
|
||||
public event Action<Vector3> ShotTraced; // 每顆子彈 (彈丸) 停下的位置,給彈道線用
|
||||
|
||||
private float nextFireTime;
|
||||
private float lastShotTime = -999f;
|
||||
@@ -157,10 +159,29 @@ public class HitscanWeapon : MonoBehaviour
|
||||
return new Vector2(horizontal, vertical);
|
||||
}
|
||||
|
||||
// 依距離由近到遠檢查 : 自己 → 略過 ; 命中框 → 命中 ; 機器人的移動膠囊 → 略過 ; 其他 (牆) → 擋住
|
||||
void ShootRay(Vector3 direction)
|
||||
{
|
||||
Vector3 origin = aimCamera.transform.position;
|
||||
Vector3 end = origin + direction * data.range; // 什麼都沒打到就畫到射程盡頭
|
||||
|
||||
RaycastHit? stop = FindFirstHit(origin, direction);
|
||||
if (stop.HasValue)
|
||||
{
|
||||
end = stop.Value.point;
|
||||
|
||||
if (stop.Value.collider.TryGetComponent(out Hitbox hitbox))
|
||||
{
|
||||
float dealt = hitbox.Hit(data.damage);
|
||||
if (dealt > 0f) HitLanded?.Invoke(hitbox.part, dealt);
|
||||
}
|
||||
}
|
||||
|
||||
ShotTraced?.Invoke(end);
|
||||
}
|
||||
|
||||
// 依距離由近到遠檢查 : 自己 → 略過 ; 命中框 → 命中 ; 機器人的移動膠囊 → 略過 ; 其他 (牆) → 擋住
|
||||
RaycastHit? FindFirstHit(Vector3 origin, Vector3 direction)
|
||||
{
|
||||
int count = Physics.RaycastNonAlloc(origin, direction, hitBuffer, data.range, ~0, QueryTriggerInteraction.Collide);
|
||||
Array.Sort(hitBuffer, 0, count, HitDistanceComparer.Instance);
|
||||
|
||||
@@ -169,24 +190,21 @@ public class HitscanWeapon : MonoBehaviour
|
||||
Collider col = hitBuffer[i].collider;
|
||||
|
||||
if (playerRoot != null && col.transform.IsChildOf(playerRoot)) continue;
|
||||
|
||||
if (col.TryGetComponent(out Hitbox hitbox))
|
||||
{
|
||||
float dealt = hitbox.Hit(data.damage);
|
||||
if (dealt > 0f) HitLanded?.Invoke(hitbox.part, dealt);
|
||||
return;
|
||||
}
|
||||
|
||||
if (col.TryGetComponent(out Hitbox _)) return hitBuffer[i];
|
||||
if (col.isTrigger || col.GetComponentInParent<BotHealth>() != null) continue;
|
||||
|
||||
return; // 被牆或其他東西擋住
|
||||
return hitBuffer[i]; // 被牆或其他東西擋住
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
void StartReload()
|
||||
{
|
||||
if (IsReloading || Ammo >= data.magazineSize) return;
|
||||
reloadEndTime = Time.time + data.reloadTime;
|
||||
|
||||
float duration = Ammo == 0 && data.emptyReloadTime > 0f ? data.emptyReloadTime : data.reloadTime;
|
||||
reloadEndTime = Time.time + duration;
|
||||
}
|
||||
|
||||
void UpdateReload()
|
||||
|
||||
@@ -31,9 +31,12 @@ public class WeaponData : ScriptableObject
|
||||
|
||||
public int magazineSize = 12;
|
||||
|
||||
[Tooltip("換彈時間 (秒)")]
|
||||
[Tooltip("戰術換彈時間 (秒) : 彈匣裡還有子彈時")]
|
||||
public float reloadTime = 1.2f;
|
||||
|
||||
[Tooltip("空彈匣換彈時間 (秒) : 打光才換,通常比較久 (Apex 也是這樣) ; 填 0 = 跟戰術換彈一樣")]
|
||||
public float emptyReloadTime = 0f;
|
||||
|
||||
[Header("後座力 : 垂直 (度/發,往上)")]
|
||||
[Tooltip("連射的前幾發")]
|
||||
public int verticalFirstShots = 5;
|
||||
|
||||
@@ -14,6 +14,12 @@ public class WeaponHUD : MonoBehaviour
|
||||
[Tooltip("彈藥 Text (TMP),例如「12 / 12」")]
|
||||
public TextMeshProUGUI ammoText;
|
||||
|
||||
[Tooltip("換彈提示 (換彈中才顯示)")]
|
||||
public GameObject reloadTips;
|
||||
|
||||
[Tooltip("換彈剩餘時間 Text (TMP),例如「1.4s」,換彈中才顯示")]
|
||||
public TextMeshProUGUI reloadTimeText;
|
||||
|
||||
[Header("命中提示")]
|
||||
public Color bodyColor = Color.white;
|
||||
public Color headColor = new Color(1f, 0.25f, 0.25f);
|
||||
@@ -25,10 +31,13 @@ public class WeaponHUD : MonoBehaviour
|
||||
private float hitMarkerTimer;
|
||||
private int shownAmmo = -1;
|
||||
private bool shownReloading;
|
||||
private int shownReloadTenths = -1; // 以 0.1 秒為單位,數字有變才更新文字
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (hitMarker != null) hitMarker.enabled = false;
|
||||
if (reloadTips != null) reloadTips.SetActive(false);
|
||||
if (reloadTimeText != null) reloadTimeText.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
@@ -44,6 +53,7 @@ public class WeaponHUD : MonoBehaviour
|
||||
|
||||
UpdateHitMarker();
|
||||
UpdateAmmoText(current);
|
||||
UpdateReloadTime(current);
|
||||
}
|
||||
|
||||
void Subscribe(HitscanWeapon weapon)
|
||||
@@ -80,5 +90,19 @@ public class WeaponHUD : MonoBehaviour
|
||||
shownAmmo = weapon.Ammo;
|
||||
shownReloading = weapon.IsReloading;
|
||||
ammoText.text = shownReloading ? "換彈中..." : $"{shownAmmo} / {weapon.data.magazineSize}";
|
||||
if (reloadTips != null) reloadTips.SetActive(shownReloading);
|
||||
if (reloadTimeText != null) reloadTimeText.gameObject.SetActive(shownReloading);
|
||||
}
|
||||
|
||||
// 換彈剩餘時間,精確到 0.1 秒 (無條件進位,避免還沒換完就顯示 0.0s)
|
||||
void UpdateReloadTime(HitscanWeapon weapon)
|
||||
{
|
||||
if (reloadTimeText == null || weapon == null || !weapon.IsReloading) return;
|
||||
|
||||
int tenths = Mathf.CeilToInt(weapon.ReloadRemaining * 10f);
|
||||
if (tenths == shownReloadTenths) return;
|
||||
|
||||
shownReloadTenths = tenths;
|
||||
reloadTimeText.text = $"{tenths / 10f:F1}s";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user