34 lines
908 B
C#
34 lines
908 B
C#
using UnityEngine;
|
|
|
|
public class PingPongMover : MonoBehaviour
|
|
{
|
|
[SerializeField] private Transform pointA;
|
|
[SerializeField] private Transform pointB;
|
|
[SerializeField] private float speed = 2f;
|
|
|
|
private Vector3 target;
|
|
|
|
private void Start()
|
|
{
|
|
if (pointA == null || pointB == null)
|
|
{
|
|
Debug.LogError("Point A and Point B must be assigned.");
|
|
enabled = false;
|
|
return;
|
|
}
|
|
|
|
target = pointB.position;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// Move toward the current target
|
|
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
|
|
|
|
// If we reach the target, switch to the other one
|
|
if (Vector3.Distance(transform.position, target) < 0.01f)
|
|
{
|
|
target = (target == pointA.position) ? pointB.position : pointA.position;
|
|
}
|
|
}
|
|
} |