using UnityEngine; namespace BinxForge.CoinRoom { [RequireComponent(typeof(Rigidbody2D))] public sealed class Patrol : MonoBehaviour { public CoinRoom room; public Vector2 start = new Vector2(-1, -2); public float minX = -1, maxX = 3, speed = 2; private Rigidbody2D body; private float direction = 1; private void Awake() { body = GetComponent(); } public void ResetPose() { if (body == null) body = GetComponent(); body.position = start; body.linearVelocity = Vector2.zero; direction = 1; } private void FixedUpdate() { if (room == null || !room.Active) { body.linearVelocity = Vector2.zero; return; } float next = body.position.x + direction * speed * Time.fixedDeltaTime; if (next >= maxX) { next = maxX; direction = -1; } else if (next <= minX) { next = minX; direction = 1; } body.MovePosition(new Vector2(next, start.y)); } private void OnTriggerEnter2D(Collider2D other) { if (room != null && room.Active && other.attachedRigidbody == room.player) room.Rules.HitPatrol(); } } }