Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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
1
Question by Mrroundtree22 · Dec 01, 2020 at 08:19 PM · raycastnavmeshagentpoint

How to detect if NavAgent has reached hit.point?

 void Update()
     {
         if (Input.GetMouseButtonDown(1))
         {
             RaycastHit hit;
 
         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
         {
             agent.SetDestination(hit.point);
 
             if (agent.destination == hit.point)
             {
                 print("Destination reached");
             }
         }
     }

The problem is the "if (agent.destination == hit.point) { print("Destination reached"); }" line. What I want is to be able to register when the agent has reached the hit.point. How do I do it?

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 OldBen1 · Dec 21, 2021 at 09:34 AM 0
Share

I'm having the same issue, did you manage to solve this?

I just want to measure if the agent has reached, or is close to reaching, the destination.

 public class GuardController : MonoBehaviour
 {
 
     public Camera cam;
 
     public NavMeshAgent agent;
 
     [SerializeField] private string selectableTag = "Guard";
 
     GameObject lastHit;
 
     bool isSelected = false;
 
 
 
     void OnMouseEnter()
     {
         CompareTag(selectableTag);
         isSelected = true;
     }
 
 
 
     // Update is called once per frame
     void Update()
 
     {
 
 
         if (Input.GetMouseButton(0) && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out var hitInfo) && isSelected)
         {
 
             GameObject.Find("People/Guard/GCube").SetActive(true);
             agent.SetDestination(hitInfo.point);
 
 // IF STATEMENT TO MEASURE IF MOVING AND REACHING LOCATION
 
             agent.gameObject.GetComponent<Animator>().Play("Walk_Male");

// PLAY IDLE ANIMATION WHEN DESTINATION IS REACHED

         }
 
         if (Input.GetMouseButtonDown(1))
         {
             GameObject.Find("People/Guard/GCube").SetActive(false);
             isSelected = false;
             agent.gameObject.GetComponent<Animator>().Play("Idle_Generic");
 
         }
 
     }
 }


,I' having the same problem. I just want to create an if statement that measures when the agent has hit or is co$$anonymous$$g close to the hit point:

 public class GuardController : MonoBehaviour
 {
 
     public Camera cam;
 
     public NavMeshAgent agent;
 
     [SerializeField] private string selectableTag = "Guard";
 
     GameObject lastHit;
 
     bool isSelected = false;
 
 
 
     void OnMouseEnter()
     {
         CompareTag(selectableTag);
         isSelected = true;
     }
 
 
 
     // Update is called once per frame
     void Update()
 
     {
 
 
         if (Input.GetMouseButton(0) && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out var hitInfo) && isSelected)
         {
 
             GameObject.Find("People/Guard/GCube").SetActive(true);
             agent.SetDestination(hitInfo.point);
 
 
 
             agent.gameObject.GetComponent<Animator>().Play("Walk_Male");
 
         }
 
         if (Input.GetMouseButtonDown(1))
         {
             GameObject.Find("People/Guard/GCube").SetActive(false);
             isSelected = false;
             agent.gameObject.GetComponent<Animator>().Play("Idle_Generic");
 
         }
 
     }
 }

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by pandolfini · Dec 01, 2020 at 08:25 PM

Your best bet is to have it s.t. your destination is "reached" when your agent is within certain bounds. Use (transform.position - destination.position).sqrMagnitude < x, where x may vary based on the speed with which your agent approaches your destination. Note that NavMesh Agents also have some properties that allow you to establish how they should target and subsequently approach a point. Consult the docs for this.

Comment
Add comment · Show 1 · 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 Mrroundtree22 · Dec 01, 2020 at 09:04 PM 0
Share

Yes I've tried that but I struggle with getting the code to understand that hit.point is the destination. The agent moves towards it no problem. Which doc should I look for?

avatar image
0

Answer by Mrroundtree22 · Dec 02, 2020 at 01:15 AM

Update: new code looks like this float dist; RaycastHit hit; void Update() { float dist = Vector3.Distance(agent.transform.position, hit.point); if (Input.GetMouseButtonDown(1)) { if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity)) { agent.SetDestination(hit.point); if (dist <.1f) { print("Destination reached"); } } }

And it doesn't work. The distance isn't really registered. The message "Destination reached" appears the second I click which is not what I want. The actual distance doesn't seem to matter at all. I want the message to only appear AFTER the agent has reached the hit.point.

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
avatar image
0

Answer by endasil_unity · Dec 21, 2021 at 11:27 AM

So what you do here is 1. store what the distance was to the target last time you pressed the mouse button and the raycast hit something. 2. Check if the mouse button is down. 3. Check if the raycast hit something. 4. Update the place the agent should travel to. 5. Check immediately if the agent reached the destination you previously clicked (and only check when the mouse button is clicked).

You will need to repeatedly check if the agent has reached its destination since it moves over time. Something like this:

 RaycastHit hit; 
 void Update()
 {
     float dist = Vector3.Distance(agent.transform.position, hit.point);
     if (dist < .1f)
     {
         print("Destination reached");
     }
     if (Input.GetMouseButtonDown(1))
     {
         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),    
                             out hit, Mathf.Infinity))
         {
                 agent.SetDestination(hit.point);    
         }
     }
 }

Futhuremore, please post the print of hit.point and agent.transform.position, should give you a hint of what went wrong or help me and others tell what is going wrong.

I see now that this is a year old post, so you have proably figured it out by now. Please post your resolution and mark it as answer so that others can learn from you.

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

200 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 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 detect if NavAgent has reached hit.point? (2) Distance doesn't work? 1 Answer

Weird navigation issue 0 Answers

If collider between two points get this point 1 Answer

Please Someone help, im pulling my hair out. 0 Answers

get the point directly above the raycast collision? 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