Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by docsavage · Feb 18, 2016 at 08:43 AM · arrayaipathfindingrandom gen

Choosing non null values from arrays for pathfinding

Hi,

I have been wrestling with this all day and would appreciate help if possible. I have game board set up using a node class that store x and z positon as ints, tiletype, and an isWalkable bool. These are added to a 2d array of nodes . eg nodeArray[xpos,zpos]; The nodes have an iswalkable bool to determine where on the board is walkable and where is not.

I want my non player characters to choose a random direction in any of the valid directions from the current node/position the npc is on whilst avoiding doubling back on itself if there are 2 or more valid directions to travel to.

The thing I am struggling with is choosing a random direction out of the valid ones whilst ignoring the null values. is there a way to do this?

If all the directions are checked and then a count done (using an int numberOfPossDirections to store value) so that Random,Range(0, numberOfPossDirections); can be used it will only return the node with the same index value and not necessarily the right node. The only thing I can think of is to change the value of the random var but this loses some of its randomess.

I originally used lists and had everything working but the I have had a 20 - 30 fps increase since converting to arrays on everything else so and am trying to optimise further. It's also become a matter of wanting to master a way of doing it old school such as in the days pre List<>.

The Lists just checked all the directions for isWalkable. if a direction was walkable it then added it to a list and then counted the size of the list and did a Random.Range(0, list.count); It then picked out the direction/node stored in the list at that position.

Any help/ideas or alternatives would be greatly appreciated.

Here is a part of the current code in c#.

Thanks

 void MoveWander()
 {
     movementDirectionInt = 0;
     bool leftH = false;
     bool rightH = false;
     bool upH = false;
     bool downH = false;


     if (currentNode.xPos > 0 && gameboardNPC.NodeArray[currentNode.xPos - 1, currentNode.zPos].isWalkable)
     {
         movementDirectionInt++;
         leftH = true;
     }
     if (currentNode.xPos < gameboardNPC.BoardWidth - 1 && gameboardNPC.NodeArray[currentNode.xPos + 1, currentNode.zPos].isWalkable) 
     {
         movementDirectionInt++;
         rightH = true;
     }
     if (currentNode.zPos < gameboardNPC.BoardHeight - 1 && gameboardNPC.NodeArray[currentNode.xPos, currentNode.zPos + 1].isWalkable) 
     {
         movementDirectionInt++;
         upH = true;
     }
     if (currentNode.zPos > 0 && gameboardNPC.NodeArray[currentNode.xPos, currentNode.zPos - 1].isWalkable) 
     {
         movementDirectionInt++;
         downH = true;
     }


     rnd = Random.Range (0, 4);
     Debug.Log ("random " + rnd);


     if (rnd == 0) {

         if (leftH && previousDirection != MovementDirection.Right) {

             //move left
             this.targetNode = gameboardNPC.NodeArray [currentNode.xPos - 1, currentNode.zPos];
             direction = MovementDirection.Left;
         }
         else rnd++;  // if not valid add 1 and try next
     }
     if (rnd == 1) {

         if (rightH && previousDirection != MovementDirection.Left) {
             //go right
             this.targetNode = gameboardNPC.NodeArray [currentNode.xPos + 1, currentNode.zPos];
             direction = MovementDirection.Right;
         }
         else rnd++; // if not valid add 1 and try next
     }
     if (rnd == 2) {
         if (upH  && previousDirection != MovementDirection.Down) {
             //go up
             this.targetNode = gameboardNPC.NodeArray [currentNode.xPos, currentNode.zPos + 1];
             direction = MovementDirection.Up;
         }
         else rnd++; // if not valid add 1 and try next
     }
     if (rnd == 3) {
         if (downH  && previousDirection != MovementDirection.Up) {
             //go down
             this.targetNode = gameboardNPC.NodeArray [currentNode.xPos, currentNode.zPos - 1];
             direction = MovementDirection.Down;
         }
         else rnd = 0;   //try previous 
     }

     if (movementDirectionInt <= 1) {  // if have to double back 

         if (previousDirection == MovementDirection.Left)
         {
             direction = MovementDirection.Right;
         }
         if (previousDirection == MovementDirection.Right)
         {
             direction = MovementDirection.Left;
         }
         if (previousDirection == MovementDirection.Up)
         {
             direction = MovementDirection.Down;
         }
         if (previousDirection == MovementDirection.Down)
         {
             direction = MovementDirection.Up;
         }
         
         targetNode = previousNode;
     }


It's quite difficult to put into words so sorry if I am not clear.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Nomenokes · Feb 19, 2016 at 12:10 AM

You could probably do a similar thing just instead of adding one to rnd try again with a new random (looping the whole section in while loop). Just make sure you test first to see if there are actually any directions (except for backwards) that you can move in (in the top of the code below). Also have like a 1000 cutoff for your random loop just in case you are reaaaally unlucky (beginning of while loop in the code below).

 bool solved=false;
 
 if(previousDirection==MovementDirection.right){
 if(rightH||upH||downH){
 solved=false;
 }else{
 solved=true;
 }
 }
 else if(previousDirection==MovementDirection.left){
 if(leftH||upH||downH){
 solved=false;
 }else{
 solved=true;
 }
 }
 //continue with testing up and down
 
 if(solved==true){//tests to see if backward was the only option
 //proceed to turn backward
 }
 
 int count=0;
 
 while(solved==false){//the big section
 
 if(count>1000){
 break;
 }
 count++;
 
 rnd=Random.Range(0,3);
 
 if(rnd==0){
 if(rightH&&previousDirection!=MovementDirection.left){
 //code
 solved=true;
 }
 }
 
 else if(rnd==1){
 //dotdotdot, same as above
 }
 
 //proceed with the rest of old script and additions
 
 }
Comment
Add comment · Show 1 · 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
avatar image Nomenokes · Feb 19, 2016 at 12:16 AM 0
Share

sorry about the smooshed code, i dont know how to indent in this

avatar image
0

Answer by docsavage · Feb 19, 2016 at 08:16 PM

Thanks for the reply @Nomenokes and taking the time to write some code out. I was too busy to test this today but I will definitely try this in the next couple of days and let you know how it works.

Really appreciate your help.

doc

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make AI objects not walk on top of each other? 5 Answers

Ai Destinations not changing when using length of raycast, 0 Answers

Artificial pathfinding with colliders 0 Answers

Spawning object with new random pathing.,Randomly moving after spawn 0 Answers

Moving Enemy Along A Star Path 1 Answer


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