60 lines
2.1 KiB
C#
60 lines
2.1 KiB
C#
using UnityEngine;
|
|
|
|
public class PlayerFireScript : MonoBehaviour
|
|
{
|
|
[Header("Снаряд")]
|
|
[SerializeField] private PickableProjectile projectilePrefab;
|
|
public bool spawnIfMissing = false;
|
|
|
|
[Header("Бросок / Подбор")]
|
|
[SerializeField] private KeyCode actionKey = KeyCode.F;
|
|
[SerializeField] private float throwSpeed = 20f;
|
|
public Transform attachPoint;
|
|
|
|
private PickableProjectile pickableProj;
|
|
|
|
void Awake()
|
|
{
|
|
if (!attachPoint)
|
|
Debug.LogWarning("PlayerFireScript: attachPoint не задан и HandPoint не найден. Будет использован корень игрока.");
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!Input.GetKeyDown(actionKey)) return;
|
|
|
|
if (pickableProj == null)
|
|
pickableProj = PickableProjectile.Instance;
|
|
|
|
if (pickableProj == null)
|
|
{
|
|
if (!spawnIfMissing || projectilePrefab == null)
|
|
{
|
|
Debug.LogError("PlayerFireScript: projectile missing; enable spawnIfMissing and assign projectilePrefab.");
|
|
return;
|
|
}
|
|
|
|
Vector3 spawnPos = attachPoint ? attachPoint.position : transform.position;
|
|
pickableProj = Instantiate(projectilePrefab, spawnPos, Quaternion.identity);
|
|
pickableProj.TryPickup(gameObject, attachPoint);
|
|
return;
|
|
}
|
|
|
|
// Бросок
|
|
if (pickableProj.CurrentState == PickableProjectile.State.Held && pickableProj.owner == gameObject)
|
|
{
|
|
float sx = transform.localScale.x;
|
|
float facing = Mathf.Sign(sx == 0 ? 1 : sx);
|
|
Vector2 dir = facing > 0 ? Vector2.right : Vector2.left;
|
|
Vector2 launchPos = (Vector2)transform.position + new Vector2(1.4f * facing, 0);
|
|
|
|
Vector3 s = pickableProj.transform.localScale;
|
|
pickableProj.transform.localScale = new Vector3(Mathf.Abs(s.x) * -facing, s.y, s.z);
|
|
|
|
pickableProj.transform.position = launchPos;
|
|
pickableProj.Launch(dir, throwSpeed, gameObject);
|
|
}
|
|
}
|
|
|
|
public void BindProjectile(PickableProjectile proj) => pickableProj = proj;
|
|
} |