109 lines
2.8 KiB
C#
109 lines
2.8 KiB
C#
|
||
using System.Collections;
|
||
using UnityEngine;
|
||
|
||
[RequireComponent(typeof(CapsuleCollider))]
|
||
|
||
public class Hole : MonoBehaviour
|
||
{
|
||
[Header("Nastavení")]
|
||
[Tooltip("Poloměr zachycení – do kolika jednotek od středu se item 'vtáhne'")]
|
||
public float catchRadius = 0.6f;
|
||
|
||
[Tooltip("Síla vtahování itemu k díře")]
|
||
public float pullForce = 4f;
|
||
|
||
[Header("Vizuál")]
|
||
public Renderer glowRenderer;
|
||
|
||
|
||
private Vector3 startPosition;
|
||
private bool isGlowing = false;
|
||
|
||
void Awake()
|
||
{
|
||
startPosition = transform.position;
|
||
|
||
CircleCollider2D col = GetComponent<CircleCollider2D>();
|
||
if (col != null)
|
||
{
|
||
col.isTrigger = true;
|
||
col.radius = catchRadius;
|
||
}
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
|
||
}
|
||
|
||
void OnTriggerStay(Collider other)
|
||
{
|
||
Debug.Log($"Trigger stay with: {other.gameObject.name}");
|
||
DraggableObject draggable = other.GetComponent<DraggableObject>();
|
||
if (draggable == null) return;
|
||
Debug.Log($"Draggable object detected: {other.gameObject.name}");
|
||
float dist = Vector3.Distance(transform.position, other.transform.position);
|
||
|
||
|
||
Rigidbody rb = other.GetComponent<Rigidbody>();
|
||
if (rb != null)
|
||
{
|
||
Debug.Log($"Rigidbody detected: {other.gameObject.name}, distance: {dist}");
|
||
Vector3 dir = (transform.position - rb.position).normalized;
|
||
rb.AddForce(dir * pullForce * Time.fixedDeltaTime, ForceMode.Impulse);
|
||
}
|
||
|
||
|
||
if (dist < catchRadius * 0.4f)
|
||
{
|
||
draggable.OnScored();
|
||
SetGlow(false);
|
||
}
|
||
}
|
||
|
||
|
||
void OnTriggerEnter(Collider other)
|
||
{
|
||
Debug.Log($"Collider entered: {other.gameObject.name}");
|
||
if (other.GetComponent<DraggableObject>() != null) SetGlow(true);
|
||
}
|
||
|
||
void OnTriggerExit(Collider other)
|
||
{
|
||
Debug.Log($"Collider exited: {other.gameObject.name}");
|
||
if (other.GetComponent<DraggableObject>() != null) SetGlow(false);
|
||
}
|
||
|
||
void SetGlow(bool active)
|
||
{
|
||
|
||
Debug.Log($"SetGlow: {active}");
|
||
|
||
isGlowing = active;
|
||
if (glowRenderer == null) return;
|
||
glowRenderer.enabled = active;
|
||
if (active) StartCoroutine(PulseGlow());
|
||
}
|
||
|
||
IEnumerator PulseGlow()
|
||
{
|
||
while (isGlowing && glowRenderer != null)
|
||
{
|
||
float t = Mathf.PingPong(Time.time * 3f, 1f);
|
||
Color c = glowRenderer.sharedMaterial.color;
|
||
glowRenderer.sharedMaterial.color = new Color(c.r, c.g, c.b, Mathf.Lerp(0.3f, 0.9f, t));
|
||
|
||
yield return null;
|
||
}
|
||
}
|
||
|
||
void OnDrawGizmosSelected()
|
||
{
|
||
Gizmos.color = Color.green;
|
||
Gizmos.DrawWireSphere(transform.position, catchRadius);
|
||
Gizmos.color = Color.yellow;
|
||
Gizmos.DrawWireSphere(transform.position, catchRadius * 0.4f);
|
||
}
|
||
}
|