Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Networking and NavMesh

Discussion in 'Multiplayer' started by yackson-cc, May 25, 2016.

?

Send NavMeshAgent.Destination() To all players

  1. network sync

    36.8%
  2. network NavMesh

    63.2%
Multiple votes are allowed.
  1. yackson-cc

    yackson-cc

    Joined:
    May 14, 2016
    Posts:
    47
    Good day!

    Need help pliss, I am doing a game with navmesh navigation, i want send navMeshAgent.destination to server and in the server to all player, for "prefect syncro", but not work good, the players move only in server.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class ClassPlayerMovement : NetworkBehaviour
    6. {
    7.     public GameObject prefabBullet;
    8.     public Transform bulletTransform;
    9.     [SyncVar]
    10.     NavMeshAgent navMeshAgent;
    11.  
    12.     void Awake()
    13.     {
    14.         navMeshAgent = GetComponent<NavMeshAgent>();
    15.     }
    16.    
    17.     // Use this for initialization
    18.     void Start ()
    19.     {
    20.    
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update ()
    25.     {
    26.         if(isLocalPlayer)
    27.         {
    28.             if(Input.GetMouseButton(1))
    29.             {
    30.                 RaycastHit tempHit;
    31.                 Ray tempRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    32.  
    33.                 if(Physics.Raycast(tempRay, out tempHit, 100, LayerMask.GetMask("layerPlatform")))
    34.                 {
    35.                     CmdScrClickAndMove(tempHit.point);
    36.                 }
    37.             }
    38.  
    39.             if(Input.GetKeyDown(KeyCode.Q))
    40.             {
    41.                 RaycastHit tempHit;
    42.  
    43.                 Ray tempRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    44.                 Physics.Raycast(tempRay, out tempHit, 100, LayerMask.GetMask("layerPlatform"));
    45.  
    46.                 CmdScrPowerQActive(new Vector3(tempHit.point.x, bulletTransform.position.y, tempHit.point.z));
    47.             }
    48.         }
    49.     }
    50.  
    51.     [Command]
    52.     void CmdScrClickAndMove(Vector3 destination)
    53.     {
    54.         navMeshAgent.SetDestination(destination);
    55.     }
    56.  
    57.     [Command]
    58.     void CmdScrPowerQActive(Vector3 direction)
    59.     {
    60.         var tempBullet = (GameObject) Instantiate(prefabBullet, bulletTransform.position, Quaternion.identity);
    61.  
    62.         tempBullet.transform.LookAt(direction);
    63.         tempBullet.GetComponent<Rigidbody>().velocity = tempBullet.transform.forward*8;
    64.         Destroy(tempBullet, 0.45f);
    65.  
    66.         NetworkServer.Spawn(tempBullet);
    67.     }
    68. }
    The players are network identity, an local player autority.

    How solve this problem?

    Thanks uniters!
     
  2. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    It's only working on the server because you're not propagating it back out to clients.

    Step A - Player chooses were to move and sends that destination to the server via a Command.
    Step B - Server verifies that's a valid place to move, and sends the destination out to all clients including the one that sent it via a ClientRpc.
    Step C - The server and clients move the player to where it's supposed to be.
     
    Tyrmuzari likes this.
  3. yackson-cc

    yackson-cc

    Joined:
    May 14, 2016
    Posts:
    47
    thanks!

    I make the script, and after comment if it's work.
     
  4. yackson-cc

    yackson-cc

    Joined:
    May 14, 2016
    Posts:
    47
    Thanks for your algorimt! this solution to works:

    Code (CSharp):
    1.  
    2. if(Input.GetMouseButton(1))
    3. {
    4.         RaycastHit tempHit;
    5.         Ray tempRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    6.  
    7.         if(Physics.Raycast(tempRay, out tempHit, 100, LayerMask.GetMask("layerPlatform")))
    8.         {//Step A
    9.                 CmdScrPlayerSetDestination(tempHit.point);
    10.         }
    11. }
    12.  
    13. [Command]
    14. public void CmdScrPlayerSetDestination(Vector3 argPosition)
    15. {//Step B, I do simple work, I not verifi a valid position in server, I only send to all clients
    16.         RpcScrPlayerSetDestination(argPosition);    
    17. }
    18.  
    19. [ClientRpc]
    20. public void RpcScrPlayerSetDestination(Vector3 argPosition)
    21. {//Step C, only the clients move
    22.         navMeshAgent.SetDestination(argPosition);
    23. }
    24.  
     
    Last edited: May 29, 2016
    DRRosen3 likes this.
  5. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Nice. Just make sure you execute the navMeshAgent.SetDestination() in the command as well so that the players move on the server as well.
     
  6. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    429
    Couldn't you just make argPosition a [syncvar] and forego the RPC? - reassigning the destination/argposition on update?

    What are the disadvantages to this approach?