Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Nov 05, 2013 at 12:46 AM by rednax20 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by rednax20 · Nov 04, 2013 at 11:53 PM · vector3addnegate

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rednax20 · Nov 05, 2013 at 12:46 AM 0
Share

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!

avatar image Huacanacha · Nov 05, 2013 at 12:51 AM 1
Share

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 :)

1 Reply

  • Sort: 
avatar image
1
Best Answer

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges