DuelSystem
Duel system for my game(needs some work)unknown
csharp
a month ago
3.1 kB
7
Indexable
using UnityEngine;
public class DuelSystem : MonoBehaviour
{
[Header("Duel")]
public Transform enemyTarget; // chest or head
public float duelTime = 3f;
[Header("Reticle")]
public RectTransform reticle;
public float reticleSpeed = 800f;
public float shrinkSpeed = 2f;
public float maxSize = 300f;
public float minSize = 60f;
public Camera cam;
[SerializeField] float _hover_time;
float timer;
bool duelActive = false;
float currentSize;
Vector2 velocity;
void Start()
{
currentSize = maxSize;
Cursor.lockState = CursorLockMode.Locked;
}
bool IsHoveringEnemy()
{
if (enemyTarget == null || Camera.main == null) return false;
Vector2 enemyScreenPos = Camera.main.WorldToScreenPoint(enemyTarget.position);
float distance = Vector2.Distance(reticle.position, enemyScreenPos);
return distance < currentSize * 0.5f; // insid e circle
}
public void StartDuel()
{
duelActive = true;
timer = duelTime;
currentSize = maxSize;
Time.timeScale = 0.5f; // slight slow-mo
}
void Update()
{
if (!duelActive) return;
HandleMovement();
HandleShrink();
timer -= Time.unscaledDeltaTime;
if (timer <= 0f || Input.GetMouseButtonDown(0))
{
ResolveShot();
}
}
void HandleMovement()
{
Vector2 input = new Vector2(
Input.GetAxis("Mouse X"),
Input.GetAxis("Mouse Y")
);
// accelerate toward input
velocity += input * reticleSpeed * Time.unscaledDeltaTime;
// apply friction (THIS creates the feel)
velocity = Vector2.Lerp(velocity, Vector2.zero, Time.unscaledDeltaTime * 2f);
// move reticle
reticle.anchoredPosition += velocity * Time.unscaledDeltaTime;
}
void HandleShrink()
{
if (IsHoveringEnemy())
_hover_time += Time.unscaledDeltaTime;
else
_hover_time = 0f;
bool locked = _hover_time > 0.2f; // must stay on target briefly
float targetSize = locked ? minSize : maxSize;
currentSize = Mathf.Lerp(currentSize, targetSize, Time.unscaledDeltaTime * shrinkSpeed);
reticle.sizeDelta = new Vector2(currentSize, currentSize);
}
void ResolveShot()
{
duelActive = false;
Time.timeScale = 1f;
// convert enemy world position screen
Vector3 screenPos = cam.WorldToScreenPoint(enemyTarget.position);
float distance = Vector2.Distance(reticle.position, screenPos);
Debug.Log("Accuracy distance: " + distance);
if (distance < 40f)
{
Debug.Log("HEADSHOT");
}
else if (distance < 120f)
{
Debug.Log("BODY SHOT");
}
else
{
Debug.Log("MISS... you dead");
}
}
}Editor is loading...
Leave a Comment