0
\$\begingroup\$

I'm trying to make a ping pong mode for the waypoints.

when I'm pressing the P key I want the transform to move between the last visited waypoint and the next waypoint.

for example, if the transform moved to waypoint index 1 and then continue to waypoint index 2 if I'm pressing on the P key while the transform is in the middle of the way to waypoint index 2 I want the transform to keep moving to waypoint index 2 and then move back to waypoint index 1 and make ping pong nonstop between the waypoints.

Same logic for any visited and next waypoints.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Mover : MonoBehaviour
{
    public List<GameObject> WayPoints;

    private int CurrentWayPoint = 0;
    private bool IsPatrolling = false;

    private void Start()
    {
        StartPatrol();
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            ReversePatrolWayPoints();
        }

        if (Input.GetKeyDown(KeyCode.P))
        {
            PingPong();
        }
    }

    public void StartPatrol()
    {
        if (!IsPatrolling)
        {
            StartCoroutine(Patrol());
        }
    }

    public void ReversePatrolWayPoints()
    {
        WayPoints.Reverse();
    }

    bool pingPongEnabled = false;
    Vector3 pingPongStart;
    public void PingPong()
    {
        pingPongEnabled = true;
    }

    private IEnumerator Patrol()
    {
        if (WayPoints == null || WayPoints.Count == 0)
        {
            Debug.Log("Waypoints list is null or empty");
            yield break;
        }

        IsPatrolling = true;

        while (CurrentWayPoint < WayPoints.Count)
        {
            if (pingPongEnabled)
            {
                transform.position = Vector3.Lerp(pingPongStart,
                    WayPoints[CurrentWayPoint].transform.position,
                    Mathf.PingPong(Time.time, 1));

                yield return null;
                continue;
            }

            float distanceToWayPoint = Vector3.Distance(transform.position, WayPoints[CurrentWayPoint].transform.position);
            transform.position = Vector3.MoveTowards(transform.position, WayPoints[CurrentWayPoint].transform.position, Time.deltaTime * 1f);

            if (distanceToWayPoint <= 1f)
            {
                pingPongStart = WayPoints[CurrentWayPoint].transform.position;

                Debug.LogFormat("Reached Waypoint {0}", CurrentWayPoint);
                CurrentWayPoint++;
                if (CurrentWayPoint >= WayPoints.Count)
                {
                    CurrentWayPoint = 0;
                }
            }

            yield return null;
        }

        IsPatrolling = false;
        yield break;
    }
}

I tried to get the last visited waypoint by referencing the visited waypoint : Line 76:

pingPongStart = WayPoints[CurrentWayPoint].transform.position;

and the next waypoint I think or thought is at Lines 63-64 :

WayPoints[CurrentWayPoint].transform.position

and the lines :

transform.position = Vector3.Lerp(pingPongStart,
                    WayPoints[CurrentWayPoint].transform.position,
                    Mathf.PingPong(Time.time, 1));

but when I'm pressing the P key the transform is moving back to the last visited waypoint and then to the next waypoint instead I want the transform to keep moving to the next waypoint and when reaching it then move back to the last visited waypoint and make ping pong between them.

The problem is that I can't make it move keep moving to the next waypoint and then back as ping pong.

\$\endgroup\$

0

You must log in to answer this question.

Browse other questions tagged .