Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
0
Question by JingoJing · Feb 18, 2019 at 05:06 AM · 2d gamedungeon

2D Obstacle Avoiding Enemies In 2D - TopDown Dungeon Help

Hey everyone,

I'm currently making a top-down dungeon crawler where enemies chase after the player, but there are obstacles around the room that they get stuck on when following their path to the player.

I've tried to use ray casts and make them decide between moving left or right to avoid obstacles, but that doesn't seem to be the perfect solution and multiple enemies get stuck on each other or get stuck in a continuous dodge loop where they keep detecting each other.

alt text

These guys here are getting stuck (highlighted in red). I was wondering if anyone had any ideas of methods to get around this?

unity-help.png (133.0 kB)
Comment
Add comment · Show 1
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 RobAnthem · Feb 18, 2019 at 05:33 AM 0
Share

Well you should raycast forward of the enemies to see if the can locate the player visually, then if not, deter$$anonymous$$e if the player is left or right of the enemies current position with something like

 public static float AngleDir(Vector2 A, Vector2 B)
 {
     return -A.x * B.y + A.y * B.x;
 }

then raycast in the corresponding direction only the relative horizontal distance, and move towards that direction if unobstructed, finally if that path is obstructed raycast towards the right and see if it is unobstructed, if that is, raycast behind and move backwards, then restart the process. But to save resources, only attempt pathfinding once the enemy has reached its current assigned destination, UNLESS that destination is the player. IE player is visible. The big thing and what most people do is create a visibility function that checks if the player is visible, and if so, sets the visibility to true. For example, if the enemy is facing away from the player, and you use a function to deter$$anonymous$$e the players angle is out of the visual angle of the enemy, you can immediately return visibility as false without ever raycasting.

An alternative would be to create a 2D grid system and half Walkable and Non-Walkable tagged sections. like 0 means walkable, 1 means non-walkable

 [1][1][1][1][1][1]
 [1][0][0][0][0][1]
 [1][0][0][1][0][1]
 [1][0][1][0][0][1]
 [1][0][0][0][0][1]
 [1][1][1][1][1][1]

Then check if player is in front, left, right, or behind, and check nodes nearest to enemy for greatest direction, if obstructed move to second greatest direction, then if all else, check opposite direction by 1 and reattempt movement. The only issue here is that enemies won't avoid eachother unless you create an enemy grid as well.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Klarzahs · Feb 18, 2019 at 11:23 AM

Hi,

have a look at the Unity NavhMesh system. This might be a little overhead for your solution, but you don't have to reinvent the wheel, aka pathfinding and collision avoidance :)

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

117 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

Related Questions

How to stop rooms from spawning on top of each other with list,How to make sure rooms don't spawn in the same place 0 Answers

Unity2D Sync falling object with animation 0 Answers

Unity Trigger 2D not working 0 Answers

My Player doesn't move anymore. I didn't change a thing in the script and it did work before. What's wrong? 1 Answer

Second camera does not show UI Image element (but does show sprite) 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