The question is answered, right answer was accepted
Scripting error
From what I understand in this script, the gameobject will relocate itself to another random spot if the position is greater than -15 AND less than 15. In other words, it will relocate itself whenever the position is between -15 and 15.
To test this, I made the script print out its location after relocating itself. But sometimes, the position is between -15 and 15. How come?
public float mxpos = 45;
public float mnpos = 15;
void Start () {
transform.position = new Vector2 (Random.Range (mxpos, -mxpos), Random.Range (mxpos, -mxpos));
while (transform.position.x > -mnpos && transform.position.x < mnpos && transform.position.y > -mnpos && transform.position.y < mnpos) {
transform.position = new Vector2 (Random.Range (mxpos, -mxpos), Random.Range (mxpos, -mxpos));
}
print(transform.position);
rb2d.velocity = new Vector2 (transform.position.x, transform.position.y);
}
I feel like I'm missing something, but I can't find it. Can anybody help? Thanks in advance.
Position is a Vector, so you want it to teleport when it is between (-15, -15) and (15, 15), right? What is the current output?
Yes, whenever it is between (-15, -15) and (15,15), I need it to move somewhere else. Currently though, the output is ranges from (-45, -45) to (45, 45). But sometimes, it ends up between (-15, -15) and (15,15). I want the script to teleport the gameobject somewhere between (-45, -45) and (45, 45) but not between (-15, -15) and (15, 15).
you could make mxpos = 30;
then
transform.position = new Vector2 (Random.Range ((mxpos+mnpos), -(mxpos+mnpos)), Random.Range ((mxpos+mnpos), -(mxpos+mnpos)));
that would make sure that it very rarely spawns in the -15 to 15 position (unless the random number generator comes up with a 0) if that is a problem, just add a 1 in the equation :) hope this helps a bit.
@buzby But wouldn't that just make it? :
transform.position = new Vector2 (Random.Range (45, -45), Random.Range (45, -45));
since it is just doing 30+15?
yes it would. and when im not quite so drunk, ill rewrite. basically though, you gotta add the + and -15 values after you generate the random number.
Answer by brunocoimbra · Jan 28, 2016 at 05:08 AM
After reading the @bubzy last reply, here is the code:
transform.position = new Vector2(
Mathf.Sign(Random.Range(-1, 1)) * (Random.Range(0, mxpos - mnpos) + mnpos),
Mathf.Sign(Random.Range(-1, 1)) * (Random.Range(0, mxpos - mnpos) + mnpos));
EDIT
Just to explain what is happening:
Mathf.Sign will return -1 if it receives a negative number and 1 if it receives a positive number OR zero.
Note also that when using "ints" in Random.Range, the max number is exclusive, so in the Random.Range(-1, 1) will always generate -1 or 0 (1 is not included, generating a 50-50 chance).
Follow this Question
Related Questions
The requested feature is not implemented. 0 Answers
Bunch of instantiated RBs blows away 0 Answers
SetSpeed float C# 1 Answer
How to find all AudioClips in Resources folder and play them? 1 Answer