• 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
2
Question by Whiskey68 · Dec 10, 2018 at 02:29 PM · networkingmultiplayernavmeshpathfindingsync

How to set NavMeshAgent Path with Vector3[] ?

The server of my multiplayer game calculates a navmesh path for NPCs. This path is then synced over the network using a serialized Vector3 array obtained through NavMeshPath.corners. However, NavMeshAgent.SetPath and NavMeshAgent.path only allow me to set a NavMeshPath. The reason I want to sync the server generated navmesh path is, to ensure that all clients use the same path for the NPC characters. I could sync the new navmesh position and set it with NavMeshAgent.SetDestination, but that wouldn't be practical regarding the "same path for all".

  • So how would I set the path with the Vector3 array?

  • Or how can I create a NavMeshPath from the Vector3 array?

  • Or what's another way of syncing the server calculated navmesh path?

Comment
Add comment · Show 5
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 BruceKristelijn · Dec 10, 2018 at 04:16 PM 0
Share

I suppose you calculate the path server side? Then I would just sync the position over network ins$$anonymous$$d of syncing the positions it should be you can see where it actually is? Or is there somehting limitating you from doing this?

avatar image Whiskey68 BruceKristelijn · Dec 10, 2018 at 04:36 PM 0
Share

I did sync the position and set it on every client with SetDestination, but due to lag compensation this does not ensure that every client calculates the same path for the respective NPC as the path starting point is different at the time of calculation on every client.

avatar image hexagonius Whiskey68 · Dec 10, 2018 at 05:46 PM 0
Share

why would you want to feed the positions back into the Nav$$anonymous$$esh if that's already the final path they walk.
just pass them along the array of points.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by muzboz · Nov 22, 2020 at 06:09 AM

I did this for my Save & Load system (for NPC nav mesh paths), and I think it works OK, and I hope it might be useful to some other people out there. Or even give people a start on how to do it even better than my version! :)


It's a little bit chopped up to fit in the relevant stuff, without too much other guff. Hopefully the useful bits are all there. :)


These methods are in a UTILS SCRIPT (for serializing Vector3's and things)

 // you can't directly serialize a Vector3, so make a struct to store the relevant info
 [System.Serializable]
 public struct SerializedVector3
 {
     public float x;
     public float y;
     public float z;
 }
 // this method converts a Vector3 into a SerializedVector3
 public static SerializedVector3 SerializeVector3(Vector3 v3)
 {
     SerializedVector3 sv3;
     sv3.x = v3.x;
     sv3.y = v3.y;
     sv3.z = v3.z;
     return sv3;
 }
 // this method converts a SerializedVector3 into a Vector3
 public static Vector3 DeserializeVector3(SerializedVector3 sv3)
 {
     Vector3 v3;
     v3.x = sv3.x;
     v3.y = sv3.y;
     v3.z = sv3.z;
     return v3;
 }


 // this method converts a path into an array of SerializedVector3's based on the path corners array.
 public static SerializedVector3[] SerializeNavPath(NavMeshPath path)
     {
         SerializedVector3[] pathCorners = new SerializedVector3[path.corners.Length];
         for (int i = 0; i < path.corners.Length; i++)
         { pathCorners[i] = Utils.SerializeVector3(path.corners[i]); }
         return pathCorners;
     }
 
 // this method converts the array of SerializedVector3's back into a normal Vector3 array, then turns that into a path using the GetCornersNonAlloc method
     public static NavMeshPath DeserializeNavPath(SerializedVector3[] sPathCorners)
     {
         NavMeshPath path = new NavMeshPath();
         Vector3[] pathCorners = new Vector3[sPathCorners.Length];
 
         for (int i = 0; i < pathCorners.Length; i++)
         { pathCorners[i] = Utils.DeserializeVector3(sPathCorners[i]); }
             
         path.GetCornersNonAlloc(pathCorners);
         return path;
     }
 

These methods are in my NPC script, for saving and loading the nav mesh path

 // this method passes the path into my save & load system
 public NPCData PrepareSaveDataForSelf()
     {
         myNPCData.savedPath = Utils.SerializeNavPath(path);
 
         return myNPCData;
     }
 
 
 // this method takes the saved path, and turns it back into a nav mesh path
 public void LoadEntityData()
     {
         path = Utils.DeserializeNavPath(myNPCData.savedPath);
     }

Comment
Add comment · Show 2 · 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 N7D · Nov 24, 2021 at 08:17 PM 0
Share

Does it really work? I don't see how this function can possibly SET path by it's description https://docs.unity3d.com/ScriptReference/AI.NavMeshPath.GetCornersNonAlloc.html

avatar image muzboz N7D · Nov 24, 2021 at 10:32 PM 0
Share

Give it a go! :D Works for me.

The wording of the Unity reference page is indeed a bit cryptic!

But yes, you give this function an array of Vector3s, and get back an array of Path Corners, which effectively make up a Path for a Nav Agent.

avatar image
0

Answer by wedge740 · Mar 24, 2019 at 12:55 PM

I have the same problem. I want to Calc the path on PC A and send it to B and C. PC A is the only one who is Calc, this is good for performance. But the only way i found out is to change the NavMeshPath.Corner readonly. Have u found a better solution?

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

199 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

Related Questions

How to serialize NavMeshPath to sync across network? 1 Answer

Unity networking tutorial? 6 Answers

Help| Flashlight sync' over network 1 Answer

Multiplayer objects isn't equals. 1 Answer

Want to purchase best multiplayer framework 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