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 GamerC4 · Jun 20, 2016 at 03:21 PM · 2daienemyenemyai

AI stops following player.

I've made an AI in my game to follow the player but for some reason, after reaching the first waypoint the AI's path isn't update and it just stops. Please help me. Here is the AI. using UnityEngine; using Pathfinding; using System.Collections;

 [RequireComponent(typeof (Rigidbody2D))]
 [RequireComponent(typeof(Seeker))]
 public class FlyingEyeballAI : MonoBehaviour
 {
     public bool AutomaticallyAware = false;
     private Vector3 v_diff;
     private float atan2;
     private int rotationSpeed = 1;
 
     public Transform target;
 
     public float updateRate = 15f;
 
     private Seeker seeker;
     private Rigidbody2D rb2d;
 
     public Path path;
 
     public float speed = 300f;
     public ForceMode2D fMode;
 
     [HideInInspector]
     public bool pathIsEnded = false;
 
     public float nextWayPointDistance = 3;
     private int currentWaypoint = 0;
     private bool searchingForPlayer = false;
     public bool idle;
     private PolygonCollider2D visionCone;
 
     void Start()
     {
        /* if(AutomaticallyAware == true)
         {
             idle = false;
         }
         else
         {
             idle = true;
         }*/
         visionCone = GetComponentInChildren<PolygonCollider2D>();
         seeker = GetComponent<Seeker>();
         rb2d = GetComponent<Rigidbody2D>();
 
         if(target == null)
         {
             if(!searchingForPlayer)
             {
                 searchingForPlayer = true;
                 StartCoroutine(SearchForPlayer());
             }
             return;
         }
 
         seeker.StartPath(transform.position, target.position, OnPathComplete);
 
         StartCoroutine(UpdatePath());
     }
 
     IEnumerator UpdatePath()
     {
         if (target == null)
         {
             if (!searchingForPlayer)
             {
                 searchingForPlayer = true;
                 StartCoroutine(SearchForPlayer());
             }
             return false;
         }
 
         seeker.StartPath(transform.position, target.position, OnPathComplete);
 
         yield return new WaitForSeconds(1 / updateRate);
         StartCoroutine(UpdatePath());
 
     }
 
     IEnumerator SearchForPlayer()
     {
         GameObject sResult = GameObject.FindGameObjectWithTag("Player");
         if (sResult == null)
         {
             yield return new WaitForSeconds(0.5f);
             StartCoroutine(SearchForPlayer());
         }
         else
         {
             target = sResult.transform;
             searchingForPlayer = false;
             StartCoroutine(UpdatePath());
             return false;
         }
     }
 
     public void OnPathComplete(Path p)
     {
         if(!p.error)
         {
             path = p;
             currentWaypoint = 0;
         }
         
     }
 
 
     void Update()
     {
         v_diff = (target.position - transform.position);
         atan2 = Mathf.Atan2(v_diff.y, v_diff.x);
         transform.rotation = Quaternion.Euler(0f, 0f, atan2 * Mathf.Rad2Deg);
     }
 
     void FixedUpdate()
     {
         if (target == null)
         {
             if (!searchingForPlayer)
             {
                 searchingForPlayer = true;
                 StartCoroutine(SearchForPlayer());
             }
             return;
         }
 
         if (path == null)
         {
             return;
         }
 
         if (currentWaypoint >= path.vectorPath.Count)
         {
             if (pathIsEnded)
             {
                 return;
             }
             StartCoroutine(SearchForPlayer());
             pathIsEnded = true;
             return;
         }
         pathIsEnded = false;
 
         Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
         dir *= speed * Time.fixedDeltaTime;
 
         if (idle == false)
         {
             rb2d.AddForce(dir, fMode);
         }
 
         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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ollobin · Jun 20, 2016 at 03:31 PM

Where exactly is the coroutine? Could we see the code for it? @GamerC4

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D Enemy Ai 0 Answers

How can I make a 4 direction animation for an enemy? 1 Answer

Vector 2 only using x (Enemy AI) 1 Answer

Trouble making jumping spider enemies 0 Answers

platform ai following player direction when close, flips normal movement ,cant get my enemy to keep his sprite position when moving back to move after following during attack 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