- Home /
The question is answered, right answer was accepted
Vector 3 do/while problem
I could barf all of my code here but, if your like me, you might skip over this question.
I have a randomly generating map script that creates a line of cubes and at the end of the line creates a city.
the city then makes another line, and that line makes a city, and that city makes a line, etc.
The script uses a for loop that creates a random amount of cubes. an empty Vector3 "cloneposition" is incremented by another Vector3 "direction".. the for loop then places a block (or a city) at cloneposition
direction is defined like this
do{
direction = Vector3(Random.Range(-1,2),0,(-1,2);
while(direction != Vector3(0,0,0); // I do this so that the line moves at least once in some diagonal or straight line
THE PROBLEM : I don't want the lines retrace themselves, if a city can a line that moves in the direction that overlaps the old line. I do not want this to happen. I decided to add a static variable "olddirection", that would be set to the negative of direction's x and z parameters every time the direction is decided upon, and then add a line that makes sure that the direction is not the same as the negated last direction.
It looks a little like this :
do{
direction = Vector3(Random.Range(-1,2),0,Random.Range(-1,2));
}while(direction == Vector3(0,0,0) && direction == olddirection);
olddirection = Vector3(direction.x * -1, 0, direction.z * -1);
It doesn't work! something must be going wrong. The boxes that are used in the line have colliders, so it is essential that they do not collide with each other (except if the lines cross at one point, that is supposed to happen). I just don't know what to do. Thank You for reading this. I would have skipped over it
Here is the rest of my code incase i did something else stupid
var thisobject : GameObject; //this object, ahh come on
var direction : Vector3;//this rows direction
var emptygameobjectprefab : GameObject; // the roads, these can change to building
var length : int;//number of segment
var cloneposition : Vector3; // this is moved to judge to position of the next piece
var city : GameObject; // the city prefab
static var numoftowns : int; // number of towns on the map
static var maxtowns = 50; // the requested number of towns
static var olddirection : Vector3; // the direction one round before it
do{
direction = Vector3(Random.Range(-1,2),0,Random.Range(-1,2));
}while(direction == Vector3(0,0,0) && direction == olddirection);
olddirection = Vector3(direction.x * -1, 0, direction.z * -1);
cloneposition = thisobject.transform.position + direction;
length = Random.Range(4,51);
if(numoftowns <= maxtowns){
for (var i = 0;i<length;i++){
Instantiate (emptygameobjectprefab, (cloneposition + Vector3(direction.x /2,direction.y,direction.z / 2)), Quaternion.identity);
cloneposition += direction;
}
Instantiate (city, (cloneposition-(direction / 2)), Quaternion.identity);
numoftowns++;
}
Thanks a bundle, have a Quishtay™ day
I'm not sure what you were trying to tell me with those point float values, but i realized my problem when I read your code. i needed an or statement!!!!!!!!, not an and statement. whether you realized it or not, you helped my a great deal thanks!
You're welcome, glad I could help!
I would still suggest you base the randomized direction on the angle of rotation around the Y axis, rather than a ZX box (this will give you a random point in a square which is not an even distribution of directions). It would also make the check for reversing directions easier. But if you're happy with the results as is that's great :)
Answer by Huacanacha · Nov 05, 2013 at 12:12 AM
I suspect there are more problems than just this, but try changing:
while(direction == Vector3(0,0,0) && direction == olddirection);
to:
while(direction == Vector3(0,0,0) || direction == olddirection); // Assuming olddirection is actually -direction from the previous move
Because Vector3's use floating points you can get almost identical values that are not equal, so your boxes may still collide.
I would rethink your approach and work with degrees rotation around the Y axis to indicate direction rather than using Vector3 (which will not give you an even distribution of directions in any case as you are asking for a point within a square). Store the previous direction, then when generating the next direction make sure this is not 180 degrees from the previous direction (with a tolerance to avoid 'almost' reverse paths). If your tolerance is +/-5 degrees you can do this directly:
yRotation = (previousYRotation+180+Random.range(0,350)) % 360;
Follow this Question
Related Questions
Move 'Player' To new position. 2 Answers
How to add 2 Quaternions. 2 Answers
How do i instantiate a Vector3[] ?? 2 Answers
Vector3 and Quaternion issues. 2 Answers
Vector3 Lerp mis-match 0 Answers