- Home /
How can i make objects to move in random speed ?
At the top of the script i have this variables
[Range(3,50)]
public float moveSpeed = 3;
public bool randomSpeed = false;
And the method with the objects movement:
private void MoveToNextFormation()
{
float step = moveSpeed * Time.deltaTime;
for (int i = 0; i < squadMembers.Count; i++)
{
squadMembers[i].transform.LookAt(newpos[i]);
if (randomSpeed == true)
{
step = Random.Range(3, 50) * Time.deltaTime;
}
squadMembers[i].transform.position =
Vector3.MoveTowards(squadMembers[i].transform.position, newpos[i], step);
if (Vector3.Distance(squadMembers[i].transform.position, newpos[i]) < threshold)
{
if (squareFormation == true)
{
Vector3 degrees = new Vector3(0, -90f, 0);
Quaternion quaternion = Quaternion.Euler(degrees);
squadMembers[i].transform.rotation = Quaternion.Slerp(squadMembers[i].transform.rotation, quaternion, rotateSpeed * Time.deltaTime);
}
else
{
squadMembers[i].transform.rotation = Quaternion.Slerp(squadMembers[i].transform.rotation, qua[i], rotateSpeed * Time.deltaTime);
}
}
}
}
I added this part for the random:
if (randomSpeed == true)
{
step = Random.Range(3, 50) * Time.deltaTime;
}
But it's not effecting the speed at all. If i change the Rang attribute value while the game is running it will change the movement speed of the characters and that is fine.
But i want now that if i check/uncheck the randomSpeed while the game is running that it will change the speed to random and if uncheck to back to it's original random that the Range attribute is set to.
Another thing i noticed using break point when it stop on the line:
step = Random.Range(3, 50) * Time.deltaTime;
I see that the value in step is float numbers like 0.454 or 0.76 or 0.343 So i wonder with this values how it should move at any speed at all ? 0.4 or 0.7 seems very low speed no? But if the randomSpeed is unchecked and the speed is used by the Range attribute it does change the speed even if the value in step is 0.456 or 0.734
So first how do i make the random speed part ? And second why the speed values are float numbers and very low ? I guess i don't understand yet how this Time.deltaTime is working ?
Answer by epicureanistik · Sep 08, 2017 at 12:40 AM
As of right now, your script is setting the speed to a new random value every frame. If you want to set it to a random value only once when you check randomSpeed, you should instead implement a custom editor window and use GUILayout.Button().
As for the float values, step in this situation is distance moved every frame, not the speed. Therefore it is normal for it to be such a small value.
Your answer
Follow this Question
Related Questions
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers
How can i change vertex posiiton to push it back -1 on z ? 1 Answer