using UnityEngine; public class MovingPlatform : MonoBehaviour { public Transform pointA; public Transform pointB; public float moveSpeed = 2f; private Vector3 nextPosition; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { nextPosition = pointB.position; } // Update is called once per frame void Update() { transform.position = Vector3.MoveTowards(transform.position, nextPosition, moveSpeed * Time.deltaTime); if (transform.position == nextPosition) { // Switch the target position nextPosition = (nextPosition == pointA.position) ? pointB.position : pointA.position; } } void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Player")) { // Attach the player to the platform collision.gameObject.transform.parent = transform; } } void OnCollisionExit2D(Collision2D collision) { if (collision.gameObject.CompareTag("Player")) { // Detach the player from the platform collision.gameObject.transform.parent = null; } } }