Mike Breidegam
Apr 10, 2020
Anyone able to get their animals to move at random speeds? I spent a lot of time and finally got them to sort of change speeds while running across the screen but not appear and run a random constant speeds.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimalMoveFwd : MonoBehaviour
{
public int [] speed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
int speedIndex = Random.Range(0, speed.Length);
transform.Translate(Vector3.forward * Time.deltaTime * speed[speedIndex]);
}
}
So the speed array I add multiple numbers for each animal. So I may set speed value to 3 and say have values for each field by 5, 10 and 15. What seems to happen is the animal toggles between speeds whilst running. A random speed value may generate (I believe), but doesn't state constant. For example it may start at 10 and then speed up to 15 and then slow down to 5 instead of staying at 10.
I think I'm closer than I originally was, when I tried to just use a Random.Range without any array.
Any help is appreciated. Online searches are giving more complex coding examples.
Thanks
// Start is called before the first frame update void Start() { speed = Random.Range(3, 15); } // Update is called once per frame void Update() {
transform.Translate(Vector3.forward * Time.deltaTime * speed); } }