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 Dinosaurstic · Jul 03, 2018 at 01:58 AM · 2draycastienumerator

How do I stop two NPCs from walking into each other and being unable to move?

I'm creating a 2D game, similar to the older generations of Pokemon or Final Fantasy. I have NPCs which walk following a grid-like pattern, using a script which was derived from the Unity 2D Roguelike tutorial. Basically, the NPCs use raycasts to check if a tile is occupied. If it isn't, they move onto it. Sometimes, though, an NPC and a different NPC both scan the same coordinate at the same time, and determine it's safe to move in, because it isn't inhabited. This leads to them both moving onto the tile, stopping them from escaping because their raycast checks always pick up the other NPC on that tile. The player can do this as well, and be in the same tile as an NPC. Are there any simple solutions to solve this?

 void Update () {
 
         //If the NPC isn't able to check if they can move, end the function
         if (!canCheck || !ableMove) return;
 
         //If the appropiate amount of time has been waited
         if (intervals >= howOftenMoves)
         {
             //The NPC can no longer move again until the functions are over
             ableMove = false;
 
             //The raycast for the Move function
             RaycastHit2D hit;
 
             //Checks whether the moving process has worked
             bool hasWorked = false;
 
             //Array of directions has been mixed up
             pd = mixer[pd];
 
             //Counting up from each possible direction from the list
             for(int i = 0; i < pd.Length; i++)
             {
                 hasWorked = Move(pd[i].xDir, pd[i].yDir, out hit);
 
                 //If the movement with the directions has worked, set the animations and stop the rest of the code
                 if (hasWorked)
                 {
                     anim.SetBool("IsMoving", true);
                     anim.SetFloat("MoveX", pd[i].xDir);
                     anim.SetFloat("MoveY", pd[i].yDir);
                     intervals = 0;
                     return;
                 }
             }
 
             //Resets the blockingLayer so that player can get out, and player cannot interact with npc
             gameObject.layer = 0;
 
             ableMove = false;
 
             intervals = 0;
 
             //Makes the NPC appear dull
             spr.material.color = new Color(0.6f, 0.6f, 0.6f);
 
             //Makes the check to see if the player is still on the npc in 5 seconds
             InvokeRepeating("TurnOnNpc", 5, 5f);
 
             //for(int i = 1; i < 4)
 
         }
 
         //Else, if the NPC is able to move, add up the intervals
         else intervals++;
 
     }


(The whole "pd mixer stuff" is just an array of X/Y coordinates that are "mixed" to randomly generate a direction to move for NPCs, so they can move randomly. )

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

0 Replies

· Add your reply
  • Sort: 

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

264 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Issue with 2D raycast 0 Answers

Raycast2D getting errors 0 Answers

Line of sight for multiple targets - AI - in a 2d game 1 Answer

Raycasting Not Working 1 Answer

How to detect if floor exists using Raycast 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