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 /
avatar image
0
Question by willaay96 · Aug 02, 2016 at 12:04 PM · unity 2daipathfinding

A* Pathfinding Enemy AI, How to search for something else when no path to player available

I've been following a tutorial on how to use A* path finding however, the game I'm making I require the enemy to search for the player, but since I will have objects to stop their path (barricades etc), I need them to first check that there is a path to the player and if not they search for a barricade to destroy and then search for the player again.

here is the current script:

 public class EnemyAlienAi : MonoBehaviour {

 public Transform target; //enemy target acquired

 public float updateRate = 4f; //how many times a second each second  we update our path

 private Seeker seeker;
 private Rigidbody2D rb;

 public Path path; //calculated path

 public float speed = 300f;
 public ForceMode2D fMode;

 [HideInInspector]
 public bool pathIsEnded = false;

 private bool searchingForPlayer = false;
 public float nextWayPointDistance = 2; //the max distance from the ai to a waypoint to continue to next waypoint
 private int currentWayPoint = 0; //waypoint currently moving towards

 void Start () {
 
     seeker = GetComponent<Seeker> ();
     rb = GetComponent<Rigidbody2D> ();

     transform.position = new Vector3 (transform.position.x, transform.position.y, 0f);

     if (target == null) {
         //Debug.Log ("No target");
         if (!searchingForPlayer) {
             searchingForPlayer = true;
             StartCoroutine (SearchForPlayer ());
         }
         return;
         //insert a search for target
     }

     seeker.StartPath (transform.position, target.position, OnPathComplete);

     StartCoroutine ("UpdatePath");

 }

 void Update(){

     var dir = target.position - transform.position;
     var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.AngleAxis(angle + 90, Vector3.forward);
 }

 IEnumerator SearchForPlayer(){
     GameObject searchResult = GameObject.FindGameObjectWithTag ("Player");
     if (searchResult == null) {
         yield return new WaitForSeconds (0.5f);
         StartCoroutine (SearchForPlayer ());
     } else {
         target = searchResult.transform;
         searchingForPlayer = false;
         StartCoroutine (UpdatePath ());
         return false;
     }
 }

 IEnumerator UpdatePath(){
     if (target == null) {
         //Debug.Log ("No target");
         if (!searchingForPlayer) {
             searchingForPlayer = true;
             StartCoroutine (SearchForPlayer ());
         }
         return false;
         //insert a search for target
     }

     seeker.StartPath (transform.position, target.position, OnPathComplete);

     yield return new WaitForSeconds (1f / updateRate);
     StartCoroutine (UpdatePath());
 }

 public void OnPathComplete(Path p){
     Debug.Log ("We have a path, did it have an error? " + p.error);
     if (!p.error) {
         path = p;
         currentWayPoint = 0;
     }
 }

 void FixedUpdate(){

     if (target == null) {
         //Debug.Log ("No target");
         if (!searchingForPlayer) {
             searchingForPlayer = true;
             StartCoroutine (SearchForPlayer ());
         }
         return;
         //insert a search for target
     }

     //TODO: always look at player?

     if (path == null) {
         return;
     }
     if (currentWayPoint >= path.vectorPath.Count) {
         if (pathIsEnded == true) {
             return;
         }
         Debug.Log ("Path reached.");
         pathIsEnded = true;
     }

     pathIsEnded = false;

     Vector3 dir = (path.vectorPath[currentWayPoint] - transform.position).normalized; // direction to next waypoint
     dir *= speed * Time.fixedDeltaTime;

     rb.AddForce (dir, fMode);//move the ai
     float dist = Vector3.Distance(transform.position, path.vectorPath[currentWayPoint]);
     if (dist < nextWayPointDistance){
         currentWayPoint++;
         return;
     }
 }

`}

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I moveTowards all the items in a list one at a time? 1 Answer

Set AI Destination Setter target from Script 2 Answers

A* Pathfinding AIFollow in JavaScript? 0 Answers

[Arongranberg A * Pathfinding Project] How to move units without overlaping of them? 1 Answer

Attacking units surround attacked building properly? (New solution, or fix to Nav Mesh?) 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