0
\$\begingroup\$

This method is being called by Update()

private void SpeedUpdater()
    {
        if (changeSpeedOnce == false)
        {
            foreach (WaypointsFollower follower in waypointsFollowers)
            {
                if (randomSpeed)
                {
                    follower.speed = Random.Range(minRandomSpeed, maxRandomSpeed);
                }
                else
                {
                    follower.speed = speed;
                }
            }

            changeSpeedOnce = true;
        }
    }

I'm using the changeSpeedOnce flag helper to assign the speed to the followers once either random or the constant speed. but then when the game is running changeSpeedOnce is true and if I want to change the randomSpeed flag again I need somehow somewhere to set the changeSpeedOnce to be false again.

I don't want to use a button or an input key for that but using the randomSpeed flag.

\$\endgroup\$
2
  • \$\begingroup\$ Solved it by using third bool flag helper and now it's working. \$\endgroup\$ Apr 25 at 23:47
  • \$\begingroup\$ That sounds like an Answer you should post below so you can Accept it. \$\endgroup\$
    – DMGregory
    Apr 25 at 23:58
0
\$\begingroup\$

The solution is to use the third bool flag.

The variable currentSpeedState , using this variable to record the current randomSpeed state if it has changed and then to set the changeSpeedOnce flag to false again.

In the Start :

currentSpeedState = randomSpeed;

Then in the SpeedUpdater :

private void SpeedUpdater()
        {
            if (changeSpeedOnce == false)
            {
                foreach (WaypointsFollower follower in waypointsFollowers)
                {
                    if (randomSpeed)
                    {
                        follower.speed = Random.Range(minRandomSpeed, maxRandomSpeed);
                    }
                    else
                    {
                        follower.speed = speed;
                    }
                }
    
                changeSpeedOnce = true;
            }
    
            if(currentSpeedState != randomSpeed)
            {
                changeSpeedOnce = false;
    
                currentSpeedState = randomSpeed;
            }
        }

And the full script :

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Cinemachine;

public class Waypoints : MonoBehaviour
{
    [Header("Objects To Move")]
    public Transform objectToMovePrefab;
    public int numberOfObjectsToMove = 1;

    [Header("Speed Settings")]
    public float speed;
    public bool randomSpeed = false;
    public float minRandomSpeed = 1;
    public float maxRandomSpeed = 100;
    private bool changeSpeedOnce = false;
    private bool currentSpeedState;

    [Header("Waypoints")]
    [SerializeField] private List<Transform> waypoints;

    [Header("Delay")]
    public float delay = 3;
    public bool randomDelay = false;
    public float minRandomDelay = 0.3f;
    public float maxRandomDelay = 5;

    [Header("LineRenderer")]
    public LineRenderer lineRenderer;
    private List<Vector3> lineRendererPositions = new List<Vector3>();

    [Header("Cinemachine Cameras")]
    public CinemachineVirtualCamera virtualCamera;

    private List<WaypointsFollower> waypointsFollowers = new List<WaypointsFollower>();

    private void Start()
    {
        currentSpeedState = randomSpeed;

        for (int i = 0; i < numberOfObjectsToMove; i++)
        {
            var parent = GameObject.Find("Moving Object Parent");
            var objectToMove = Instantiate(objectToMovePrefab, parent.transform);
            objectToMove.name = "Platfrom";

            waypointsFollowers.Add(objectToMove.GetComponent<WaypointsFollower>());
        }

        virtualCamera.Follow = waypointsFollowers[0].gameObject.transform;
        virtualCamera.LookAt = waypointsFollowers[0].gameObject.transform;

        SpeedUpdater();

        StartCoroutine(SendObjectstomoveWithDelay());
    }

    private void Update()
    {
        GetLinePointsInWorldSpace();

        SpeedUpdater();
    }

    public int Count => waypoints.Count;
    public Vector3 GetWaypoint(int index)
    {
        return waypoints[index].position;
    }

    IEnumerator SendObjectstomoveWithDelay()
    {
        {
            foreach (WaypointsFollower follower in waypointsFollowers)
            {
                if (randomDelay)
                {
                    delay = Random.Range(minRandomDelay, maxRandomDelay);
                }

                yield return new WaitForSeconds(delay);

                follower.go = true;
            }
        }
    }

    private void SpeedUpdater()
    {
        if (changeSpeedOnce == false)
        {
            foreach (WaypointsFollower follower in waypointsFollowers)
            {
                if (randomSpeed)
                {
                    follower.speed = Random.Range(minRandomSpeed, maxRandomSpeed);
                }
                else
                {
                    follower.speed = speed;
                }
            }

            changeSpeedOnce = true;
        }

        if(currentSpeedState != randomSpeed)
        {
            changeSpeedOnce = false;

            currentSpeedState = randomSpeed;
        }
    }

    List<Vector3> GetLinePointsInWorldSpace()
    {
        lineRendererPositions = new Vector3[lineRenderer.positionCount].ToList();
        //Get the positions which are shown in the inspector 
        lineRenderer.GetPositions(lineRendererPositions.ToArray());


        //the points returned are in world space
        return lineRendererPositions;
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ So, instead of changing changeSpeedOnce when you need to reset the speed, you now change currentSpeedState that depends on the current value of randomSpeed which needs to be changed anyway to reset the speed... Wouldn't it be easier to just call SpeedUpdater() only when you actually need it, i.e., only when you change the randomSpeed value, instead of calling it in every frame? It seems like you need it only once in a while and the most of the time it's just called to do nothing. \$\endgroup\$
    – Ermiq
    May 27 at 5:28
  • \$\begingroup\$ I mean, it seems like you change randomSpeed somewhere else anyway. So, you could just make a method void ResetRandomSpeed() { randomSpeed = !randomSpeed; SpeedUpdater(); } And remove the SpeedUpdater() from Update() loop. \$\endgroup\$
    – Ermiq
    May 27 at 5:33

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.