0
\$\begingroup\$

I want to decide if the speed will be randomly or constant.

In the top of script:

[Header("Speed")]
public float constantSpeed;
public float minRandomSpeed, maxRandomSpeed;
public bool randomSpeed = false;

In Start:

private void Start()
    {
     RandomSpeed();
    }

In Update

private void Update()
    {
        RandomSpeed();
    }

RandomSpeed:

private void RandomSpeed()
    {
        for (int i = 0; i < instancesToMove.Length; i++)
        {
            if (randomSpeed)
            {
                movementSpeeds[i] = Random.Range(minRandomSpeed, maxRandomSpeed);
            }
            else
            {
                movementSpeeds[i] = constantSpeed;
            }
        }
    }

The problem is in the RandomSpeed if randomSpeed is true it will keep giving the movementSpeeds each frame a new random values. But I want that when randomSpeed is true give movementSpeeds new speed values only once. Then if I will set the randomSpeed false then true again then give again new random speed values.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Don't do this:

private void Update()
{
    RandomSpeed();
}

The only thing you should do during Update() regarding speeds is to check to see if the value of randomSpeed has changed, and if so, generate new values (assuming that you want the boolean to change at runtime). You do not, and should not, generate new speed values every frame.

\$\endgroup\$
4
  • \$\begingroup\$ Do I need another helper flag ? If I set randomSpeed to true it will keep change the values all the time. Even if not calling RandomSpeed at Update once I set randomSpeed to true it will change the values in any case each frame. I don't want to change randomSpeed to true and then set it back to false. I want to keep randomSpeed true once it's true so the user will know it's on randomSpeed mode. \$\endgroup\$
    – Daniel Lip
    Commented Mar 13, 2018 at 21:42
  • \$\begingroup\$ it will change the values in any case each frame How? You're not calling any code in Update() \$\endgroup\$ Commented Mar 13, 2018 at 21:59
  • \$\begingroup\$ If I check the value of randomSpeed if it's true or false in Update I should also call to the RandomSpeed method to generate each time a new values. If random to generate new values if not random then the constant speed. But I need ot call RandomSpeed to generate the values and if in Update randomSpeed is true it will keep generate new values all the time. "assuming that you want the boolean to change at runtime" Right I want it to change in runtime. \$\endgroup\$
    – Daniel Lip
    Commented Mar 13, 2018 at 22:17
  • \$\begingroup\$ You need to check to see if the value of randomSpeed has changed. You don't give a shit about it's current value in Update(), you care about it's current value not equaling its value last frame. This requires a second boolean. \$\endgroup\$ Commented Mar 13, 2018 at 22:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .