I’m trying to simulate basic crowd movement and could use some help with optimization.
I have several game objects as crowds and want them to just move up or down randonmly.
I don’t want to use animations as they take up too much resource with performance. As it’s a simple movement, doing the movement via code seems ideal.
I’m using the following code to try and replicate my movement.
private float yPos;
private float speed = 1f;
private bool goingUp = false;
private bool startMovement = false;
void Start()
{
yPos = transform.localPosition.y;
StartCoroutine(StartingMovement());
}
IEnumerator StartingMovement()
{
yield return new WaitForSeconds(Random.Range(0.5f, 4));
startMovement = true;
}
private void Update()
{
if (startMovement == true)
{
if (goingUp)
{
transform.position += Vector3.up * speed * Time.deltaTime;
if (transform.position.y > yPos + 0.4f)
{
goingUp = false;
}
}
else
{
transform.position += Vector3.down * speed * Time.deltaTime;
if (transform.position.y < yPos)
{
goingUp = true;
}
}
}
}
This does work in the sense that each object does move up and down randomly, however this doesn’t seem very optimized as this script is attached to each object to order to do this. As a result, I get significant delays when switching scenes.
Is there a more optimal/better way of going about this, where i can get multiple child objects to move randomly?
I did think of just using a script of the parent and access the child objects, but i can’t seem to make them move individually.
I appreciate that this may not be the best way to go about it so any input would be helpful.
Test project: Crowd Test (1).zip - Google Drive