1
\$\begingroup\$

What i'm trying to do is once the random flag is true move the player between the waypoints randomly. but just calling the random method is not enough.

using UnityEngine;
using System.Collections;

public class Waypoints : MonoBehaviour
{
    public GameObject[] waypoints;
    public Transform target;
    public float moveSpeed = 10f;
    public float slowDownSpeed = 3f;
    public float reverseSlowDownSpeed = 3f;
    public float rotationSpeed = 1f;
    private int targetsIndex = 0;
    private Vector3 originalPosition;
    private GameObject[] players;

    public Transform reverseTarget;
    private int reverseTargetsIndex = 0;
    private Vector3 reverseOriginalPosition;

    public bool random = false;

    // Use this for initialization
    void Start()
    {
        waypoints = GameObject.FindGameObjectsWithTag("Blocks");
        players = GameObject.FindGameObjectsWithTag("Player");
        originalPosition = players[0].transform.localPosition;
    }

    // Update is called once per frame
    void Update()
    {
        if (random == true)
        {
            RandomWayPointsAI();
        }
        else
        {
            WayPointsAI();
        } 
    }

    private void WayPointsAI()
    {
        if (targetsIndex == waypoints.Length)
            targetsIndex = 0;
        target = waypoints[targetsIndex].transform;
        float distance = Vector3.Distance(players[0].transform.position, target.transform.position);
        players[0].transform.localRotation = Quaternion.Slerp(players[0].transform.localRotation, Quaternion.LookRotation(target.position - players[0].transform.localPosition), rotationSpeed * Time.deltaTime);

        //move towards the player
        if (distance < 30)
        {
            players[0].transform.localPosition += players[0].transform.forward * slowDownSpeed * Time.deltaTime;
        }
        else
        {
            players[0].transform.localPosition += players[0].transform.forward * moveSpeed * Time.deltaTime;
        }
        if (distance < target.transform.localScale.magnitude)
        {
            targetsIndex++;
        }
    }

    private void ReverseWayPointsAI()
    {
        if (reverseTargetsIndex == 0)
            reverseTargetsIndex = waypoints.Length - 1;
        reverseTarget = waypoints[reverseTargetsIndex].transform;
        float distance = Vector3.Distance(players[1].transform.position, reverseTarget.transform.position);
        players[1].transform.rotation = Quaternion.Slerp(players[1].transform.rotation, Quaternion.LookRotation(reverseTarget.position - players[1].transform.position), rotationSpeed * Time.deltaTime);

        //move towards the player
        if (distance < 30)
        {
            players[1].transform.position += players[1].transform.forward * reverseSlowDownSpeed * Time.deltaTime;
        }
        else
        {
            players[1].transform.position += players[1].transform.forward * moveSpeed * Time.deltaTime;
        }
        if (distance < reverseTarget.transform.localScale.magnitude)
        {
            reverseTargetsIndex--;
        }
    }

    void RandomWayPointsAI()
    {
        if (random == true)
        {
            int index = Random.Range(0, waypoints.Length);
            target = waypoints[index].transform;
        }
    }

    void DrawLinesInScene()
    {
        // draw lines between each checkpoint //
        for (int i = 0; i < waypoints.Length - 1; i++)
        {
            Debug.DrawLine(waypoints[i].transform.position, waypoints[i + 1].transform.position, Color.blue);
        }

        // draw a line between the original transform start position 
        // and the current transform position //
        Debug.DrawLine(originalPosition, players[0].transform.position, Color.red);
        Debug.DrawLine(reverseOriginalPosition, players[1].transform.position, Color.red);

        // draw a line between current transform position and the next waypoint target
        // each time reached a waypoint.
        if (target != null)
            Debug.DrawLine(target.transform.position, players[0].transform.position, Color.green);
        if (reverseTarget != null)
            Debug.DrawLine(reverseTarget.transform.position, players[1].transform.position, Color.green);
    }

    void AddColliderToWaypoints()
    {
        foreach (GameObject go in waypoints)
        {
            SphereCollider sc = go.AddComponent<SphereCollider>() as SphereCollider;
            sc.isTrigger = true;
        }
    }
}

Inside the Update i'm checking if random is true then calling the RandomWayPointsAI(); but it's not moving the player it's just keep picking up each frame a new random waypoint but that's it.

void Update()
    {
        if (random == true)
        {
            RandomWayPointsAI();
        }
        else
        {
            WayPointsAI();
        } 
    }
\$\endgroup\$
  • 1
    \$\begingroup\$ because Update function called each frame so your function called each frame, you need once run in update function.Define a variable as a checker When the function is called, you must make it false onceRun = false; something like this if (random && onceRun) \$\endgroup\$ – Seyed Morteza Kamali Dec 13 '17 at 3:43
1
\$\begingroup\$

Just change your update function to

void Update()
{
        WayPointsAI();
}

and your waypointsAI() to

private void WayPointsAI()
{
    if (targetsIndex == waypoints.Length)
        targetsIndex = 0;
    target = waypoints[targetsIndex].transform;
    float distance = Vector3.Distance(players[0].transform.position, target.transform.position);
    players[0].transform.localRotation = Quaternion.Slerp(players[0].transform.localRotation, Quaternion.LookRotation(target.position - players[0].transform.localPosition), rotationSpeed * Time.deltaTime);

    //move towards the player
    if (distance < 30)
    {
        players[0].transform.localPosition += players[0].transform.forward * slowDownSpeed * Time.deltaTime;
    }
    else
    {
        players[0].transform.localPosition += players[0].transform.forward * moveSpeed * Time.deltaTime;
    }
    if (distance < target.transform.localScale.magnitude)
    {
        if(random)
            targetsIndex = Random.Range(0, waypoints.Length);//you can do more 
                     //logic here to prevent from same targetsIndex value.
        else
            targetsIndex++;
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.