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 coneys · Nov 08, 2016 at 07:35 AM · navigationagent

Nav Agent multiple Units problem

Hello everyone.

I got a problem with Nav Agent in Unity 5. Everythink works perfectly well for 4-5 units, but when i select more they act like crazy. I tried to debug and I am sure that everyone gets good target. Don't know if it is important, but this is mobile game.

My navigation script: (I'm also creating animation in target place, that is why GameObject is made there) using UnityEngine; using System.Collections; using System;

public class TouchNavigation : Interaction { private GameObject clickObj; public GameObject Click; public Transform location;

 public float RelaxDistance = 10;
 private NavMeshAgent agent;
 private Vector3 target = Vector3.zero;
 private bool selected = false;
 private bool isActive = false;
 private Vector3 change = Vector3.zero;
 private Vector3 firstTarget = Vector3.zero;
 public Vector3 RotateV = Vector3.zero;
 private bool rotateDone = false;
 private bool isCreated = false;
 private float timesincestart = 0;








 public override void Dselect()
 {
     selected = false;
 }

 public override void Select()
 {
     selected = true;
 }

 public void sendToTarget()
 {
     Debug.Log("Called, target is:  " + target);

     agent.SetDestination(target);
     agent.Resume();
     isActive = true;

 }
 private bool closeto(Vector3 start, Vector3 finish, Vector3 change)
 {

     if ((finish.x > start.x - change.x && finish.x < start.x + change.x) && (finish.y > start.y - change.y && finish.y < start.y + change.y) && (finish.z > start.z - change.z && finish.z < start.z + change.z))
     {
         return true;
     }

     return false;
 }
 void Start()
 {
     clickObj = null;
     agent = GetComponent<NavMeshAgent>();
     change.x = 20;
     change.y = 20;
     change.z = 20;
 }

 // Update is called once per frame
 void Update()
 {
     if (Input.touchCount == 1)
     {

         if (Input.GetTouch(0).phase == TouchPhase.Began && selected)
         {
             timesincestart = Time.time;

             firstTarget = RtsManager.Current.ScreenPointToMapPosition(Input.GetTouch(0).position);
             if (firstTarget == Vector3.zero)
                 return;
             var ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
             RaycastHit hit;
             Physics.Raycast(ray, out hit);
             var interact = hit.transform.GetComponent<Interactive>();
             if (interact != null)
                 return;

             Vector3 clickSpot = firstTarget;
             clickSpot.y = 10;

             if (isCreated == false)
             {

                 clickObj = (GameObject)GameObject.Instantiate(Click, clickSpot, location.rotation);
                 rotateDone = false;
                 isCreated = true;
             }

         }

         if (Input.GetTouch(0).phase == TouchPhase.Ended)
         {
             if (isCreated)
             {
                 Destroy(clickObj);
                 isCreated = false;
                 rotateDone = true;
                 return;
             }

         }
     }
     if (selected == false && isCreated == true)
     {
         Destroy(clickObj);
         isCreated = false;
         rotateDone = true;
         return;
     }
     if (selected && Input.touchCount != 0)
     {
         var tempTarget = RtsManager.Current.ScreenPointToMapPosition(Input.GetTouch(0).position);
         if (tempTarget != Vector3.zero)
         {
             if (rotateDone == false)
             {
                 if (clickObj != null)
                 {
                     clickObj.transform.Rotate(RotateV * Time.deltaTime * 4);
                     clickObj.transform.position = tempTarget;
                 }
             }
             if (!(closeto(firstTarget, tempTarget, change)))
             {
                 Destroy(clickObj);
                 rotateDone = true;
                 isCreated = false;
                 return;
             }
             float timenow = Time.time;
             if (timenow - timesincestart >= 1)
             {
                 rotateDone = true;
                 if (closeto(firstTarget, tempTarget, change))
                 {
                     target = tempTarget;
                     sendToTarget();

                 }
                 else
                 {
                     Destroy(clickObj);
                     rotateDone = true;
                     isCreated = false;
                     return;

                 }
             }

         }
     }

     if (isActive && Vector3.Distance(target, transform.position) < RelaxDistance)
     {

         agent.Stop();
         isActive = false;
     }

 }

}

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 to queue NavMeshAgents on entering a tile in Unity (based on path distance to tile)? 0 Answers

NavMesh Agent rotation problem 3 Answers

Making NavMesh areas? 0 Answers

Why Nav Mesh Agent has been baked intermittently on 1 straight component? 0 Answers

How to generate Nav Mesh floor in a procedural generated maze? 3 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