using UnityEngine; public class BoundaryDestroy : MonoBehaviour { private float targetBoundary; // 目标边界 private bool movingRight; // 角色是否向右移动 public void SetBoundary(float boundary, bool isMovingRight) { targetBoundary = boundary; movingRight = isMovingRight; } void Update() { // 向右移动时,超出右边界销毁 if (movingRight && transform.position.x > targetBoundary) { Destroy(gameObject); } // 向左移动时,超出左边界销毁 else if (!movingRight && transform.position.x < targetBoundary) { Destroy(gameObject); } } }