• 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
Question by Dodokom · Jul 26, 2019 at 01:08 PM · enemycolliderspathfindingenemy aiastar

How to make enemies take wider routes?

I'm currently using A* pathfinding to mainly control my enemy's movement, however, I'm having a problem with it being constantly in contact with the collider, and it struggling to get around the corners. I would make the margins wider, but the problem there is that its destination is next to the collider, so that's not an option. The yellow boxes in the image are the potential destinations. Any advice or ideas? Thanks in advance! alt text

example.png (235.4 kB)
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by blinkafrootable · Jul 26, 2019 at 02:56 PM

I suggest that you change the A* algorithm to not allow diagonal movement if there is an object adjacent to the enemy. T$$anonymous$$s image demonstrates what I'm talking about: alt text If you're wondering how exactly you might implement that in code then I'd like to see the code you used for the algorithm.

Comment

People who like this

0 Show 3 · 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 Dodokom · Jul 26, 2019 at 03:58 PM 0
Share

I thought unticking cut corner was supposed to do that? Regardless, here's the part of my code that uses A*:

 using UnityEngine;
 using Pathfinding;
 
 public class Temp : MonoBehaviour
 {
     //pathfinding variables
     Seeker seeker;
     public Path path;
     public float nextWaypointDistance = 3;
     private int currentWaypoint = 0;
     public bool reachedEndOfPath;
     Vector2 dir;
     public float pathUpdateRate = 2f;
 
     //cover-checking variables
     Collider2D[] coversNearby = new Collider2D[50];
     Collider2D closestCover;
     float closestCoverDistance = 15f;
     public static bool inCover;
 
     void Update()
     {
         if (path != null)
         {
             reachedEndOfPath = false;
             float distanceToWaypoint;
             while (true)
             {
                 distanceToWaypoint = Vector2.Distance(transform.position, path.vectorPath[currentWaypoint]);
                 if (distanceToWaypoint < nextWaypointDistance)
                 {
                     if (currentWaypoint + 1 < path.vectorPath.Count)
                     {
                         currentWaypoint++;
                     }
                     else
                     {
                         reachedEndOfPath = true;
                         break;
                     }
                 }
                 else
                 {
                     break;
                 }
             }
             dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
         }
     }
 
     void FixedUpdate()
     {
 
         if (closestCover != null)
         {
             rb2d.AddForce((dir) * movementSpeed);
         }
     }
 
     void GetClosestViableCover()
     {
         closestCoverDistance = 15f;
 
         int numberOfCovers = Physics2D.OverlapCircleNonAlloc(transform.position, 15f, coversNearby, mask2);
         for (int i = 0; i < numberOfCovers; i++)
         {
             float distanceFromCover = Vector2.Distance(coversNearby[i].transform.position, transform.position);
             if (distanceFromCover < closestCoverDistance)
             {
                 closestCoverDistance = distanceFromCover;
                 closestCover = coversNearby[i];
                 seeker.StartPath(transform.position, closestCover.transform.position, OnPathComplete);
             }
 
         }
     }
 
     public void OnPathComplete(Path p)
     {
         if (!p.error)
         {
             path = p;
             currentWaypoint = 0;
         }
     }
 
 }

avatar image blinkafrootable · Jul 26, 2019 at 04:40 PM 0
Share

Where in your code is the boolean cut corner implemented? I don't see anything that indicates that the code checks to see if adjacent waypoints are open

avatar image Dodokom blinkafrootable · Jul 26, 2019 at 04:59 PM 0
Share

It's not in my own script, it's in the "Astar Path" script. alt text

example2.png (30.7 kB)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

116 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

Related Questions

How to I make FindGameObjectWithTag() not just find itself? 1 Answer

Inaccurate A* Pathfinding Scan! 3 Answers

How do I go about making a 2d platform enemy ai 1 Answer

Looping Between Waypoints 1 Answer

Astar Pathfinding with unity 2d 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