- Home /
How do I get multiple Random.ranges from one script
I'm trying to get a bunch of npc's to patrol randomly. I use Random.range to spawn a new position, and then i move the npc to that new position. I have the same script on all the NPC's, so they're using the same Random.Range, so they end up going to the same position. I'm using Random.Range to control how long they pause for inbetween moving, and again, they pause for the same amount of time. I think its important they use the same script, because I want to use lots of npc's in the one scene, and i dont want to make a new script for every npc. I'm really stumped. Is there a standard way of managing lots of randomness? I cant seem to find one.
Heres my script:
private Animator animator;
private SpriteRenderer spriteRenderer;
public float speed;
private float waitTime;
public float startWaitTimeMin;
public float startWaitTimeMax;
public Transform moveSpot;
public float minX;
public float maxX;
public float minY;
public float maxY;
void Start()
{
animator = GetComponent<Animator>();
spriteRenderer = GetComponent<SpriteRenderer>();
waitTime = Random.Range(startWaitTimeMin, startWaitTimeMax);
moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position,
moveSpot.position, speed * Time.deltaTime);
if (Vector2.Distance(transform.position, moveSpot.position) < 0.2f)
{
if (waitTime <= 0)
{
moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
animator.SetBool("isWalking", true);
waitTime = Random.Range(startWaitTimeMin, startWaitTimeMax);
}
else
{
animator.SetBool("isWalking", false);
waitTime -= Time.deltaTime;
}
}
}
I would say something else is going on. Using Random.Range in Start() will produce different results - and in the Update (even if multiple gameObjects using the same script). if it didn't, that would be pretty broken.
You might be right, If i use a different moveSpot position for each NPC, they do seem to move differently.
I need to find somewhere to read up about how Random.range works lol
Answer by Bunny83 · Aug 03, 2019 at 01:33 AM
Random.Range will return a different random value each time it is called. However you seem to reference a gameobject with your "moveSpot" reference. Are you sure that each of your NPC has it's own Transform object? If they all use the same object, of course they will all move to the same position. When each NPC is setting the position of that one object, they will just overwrite each other and only the last assignment would take effect.
Why do you actually use a Transform? Since you only need a target location, you probably should simply store the target position in a Vector3 variable, so each NPC has it's own target. If for some reason you want / need to use an actual gameobject for the target position, make sure every NPC has it's own moveSpot gameobject and that each NPC is actually referencing their own.
I copied the majority of this script off of a YouTube tutorial, and have been playing around with it. I've been wondering about using another objects Transform, but hadn't got to it yet.
$$anonymous$$y first solution was to use different 'moveSpots' as there initial Transform, but it feels really clunky having to make new 'moveSpot' for each NPCs.
Thanks for the tip.
Your answer
Follow this Question
Related Questions
Use multiple NavMesh ? -1 Answers
(2D) How to shoot multiple bullets in different directions 1 Answer
Multiple directional lights with shadows 1 Answer
laser is not working 0 Answers
Animating 2D Character made up of 2 or more swapable sprites 1 Answer