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 /
This question was closed Feb 16, 2016 at 04:33 AM by Bioder for the following reason:

no answers

avatar image
0
Question by Bioder · Feb 11, 2016 at 12:12 AM · c#crashnavmeshagentdistancefreeze

Keep distance using NavMeshAgent causing freeze.

My "teammate" is using NavMeshAgent to follow player. In unity 5.3.2f1(newest) i can set "Stopping distance" but there is no option to set Keep distance (what should be implemented). The way my teammate is kepping distance is bad cause he is just teleporting back (maybe that's the issue) but i think no matter what i put there it will freeze: http://i.imgur.com/7SHLx5o.gif

How to make this to not freeze whole unity?

 if (distance < 2)
             {
                 Debug.Log(distance);
                 distance = 2;
                 transform.position = (transform.position - target.transform.position).normalized * distance + target.transform.position;
             }

Full code:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof (NavMeshAgent))]
 public class Teammate : MonoBehaviour {
 
     NavMeshAgent pathfinder;
     Transform target;
     float distance = 0;
 
     void Start () {
         pathfinder = GetComponent<NavMeshAgent> ();
         target = GameObject.FindGameObjectWithTag ("Player").transform;
 
         StartCoroutine(UpdatePath());
     }
 
     void Update () {
     }
 
     IEnumerator UpdatePath()
     {
         float refreshRate = .25f;
 
         while (target != null)
         {
             distance = Vector3.Distance(this.transform.position, target.transform.position);
 
             if (distance < 2)
             {
                 Debug.Log(distance);
                 distance = 2;
                 transform.position = (transform.position - target.transform.position).normalized * distance + target.transform.position;
             }
             else {
                 Vector3 targetPosition = new Vector3(target.position.x, 0, target.position.z);
                 pathfinder.SetDestination(targetPosition);
                 yield return new WaitForSeconds(refreshRate);
             }
         }
     }
 }
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 Bioder · Feb 11, 2016 at 09:31 AM 0
Share

Got an update for follow and flee. It's moving away smoothly but always to the exact same point even if it's randomized ...why ?? With random value, $$anonymous$$mmate is going somewhere near the cube. http://i.imgur.com/67F49DQ.gif

 if (distance < 2)
             {
                 Vector3 targetPosition = new Vector3(Random.Range(-10.0F, 10.0F), 0, Random.Range(-10.0F, 10.0F));
                 targetPosition.Normalize();
                 pathfinder.SetDestination(targetPosition);
                 yield return new WaitForSeconds(refreshRate);
             }
 else
             {
                 Vector3 targetPosition = new Vector3(target.position.x, 0, target.position.z);
                 pathfinder.SetDestination(targetPosition);
                 yield return new WaitForSeconds(refreshRate);
             }

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Sending an email from my project crashes the unity editor 0 Answers

Why does Unity 2017.3.0f3 crash when NavMeshAgent is scripted to transform.Translate 2 Answers

C# Drag rotation code crashing unity with no errors. 1 Answer

Multiple Cars not working 1 Answer

Unity randomly going unresponsive when importing script changes 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