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 LeoSaurusRex · Apr 12, 2016 at 04:39 PM · ainavmeshagentnoobfinite-state-machine

why does my agent keep circling around a previsoulsy set destination?

when you click on the question title thing, it says "type a question relevant to the community"... so i immediately get the urge to ask something like: what is your favorite pizza topping??? now to get it out of the way: yes, I'm a huge noob, i understand what the script means and what goes where but I'm in no way classified to write it on my own off the top of my head (YET). anyway! i have this simple FSM (if it classifies as one) that i fished out from a tutorial on youtube, now, i tell my agent (called behemoth cuz it sounds cool) to set a target after the player is gone, this would be it's "home". so, the player shows up in front of the raycast, the agent charges in his direction, realizes the player is no longer in front of him and decides its time to go home. now up until this point its working, but if you were to stand in front of him again he'll charge but with the home still as its target, so it ends up doing donuts around the home, even though the "charge" state doesn't have a set destination. i spent an entire day trying to rwrite this in different ways (even without an fsm, hardcoding everythin, twas a pain) to no avail. please, if you are kind hearted as to let me know what i did wrong, i apreciate it.

 using UnityEngine;
 using System.Collections;
 
 public class Controller_Behemoth : MonoBehaviour {
 
     public Transform target;
     private NavMeshAgent navComponent;
     private bool alive = true;
     public float Distance;
     private Rigidbody rb;
     public int chargeSpeed;
     public float cooltime = 3;
 
     //FSM
     public State state;
     public enum State
     {
         IDLE,
         CHARGING,
         RETURNING
     }
         
     // Use this for initialization
     void Start () 
     {
         //rb = GetComponent<Rigidbody> ();
 
         //target = GameObject.FindGameObjectWithTag ("Player").transform;
         StartCoroutine (FSM ());
         navComponent = this.gameObject.GetComponent<NavMeshAgent> ();
         state = State.IDLE;
     }
 
     IEnumerator FSM()
     {
         while (alive) {
             switch (state) {
             case State.IDLE:
                 idle ();
                 break;
             case State.CHARGING:
                 charging ();
                 break;
             case State.RETURNING:
                 returning ();
                 break;
             }
             yield return null;
         }
     }
 
     void idle()
     {
     }
 
     void Update()
 
     {
         RaycastHit hit;
         Debug.DrawRay (transform.position, transform.forward, Color.green);
         if (Physics.Raycast (transform.position, transform.forward, out hit))
         {
             if (hit.collider.gameObject.tag == "Player") {
                 print ("meaw");
             
                 state = State.CHARGING;
             }
         }
     }
 
     void returning() 
     {
         if (state == State.RETURNING) {
             navComponent.SetDestination (target.position);
         } else {
             navComponent.SetDestination (this.transform.position);
         }
 
 //        if (gameObject.transform.position == target.position) 
 //        {
 //            state = State.IDLE;
 //        }
     }
 
     void charging()
     {
         cooltime -= Time.deltaTime;
         transform.Translate (Vector3.forward * chargeSpeed * Time.deltaTime, Space.Self);
         if (cooltime <= 0) {
             cooltime = 3;
             state = State.RETURNING;
         }
     }
 }
 
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

There is no argument given that corresponds to the required formal parameter 'newPosition' of 'NavMeshAgent.Warp(Vector3)' 0 Answers

navmesh agent not working after update to 2017.1 3 Answers

Why does my Nav Mesh Agent walk to the wrong side of its destination object? 0 Answers

NavMeshAgent isn't chasing the player 0 Answers

Multiple AI agents going through the same Nav Mesh Link at the same time ? 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