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

Client not syncing.

Discussion in 'Multiplayer' started by Moose13, Apr 9, 2019.

  1. Moose13

    Moose13

    Joined:
    Jan 17, 2019
    Posts:
    1
    Hello,

    I creating top down tank multiplayer game and I using UNet method. My client doesnt syncing moving and shooting to server but server syncing it to client.

    My player (Tank) prefab has components: MoveTank Script, NetworkIdentity, NetworkTransform, Rigidbody 2D, Box Colider 2D

    My player (Tank) prefab has child: Turret
    And Turret has child: BulletPosition
    My Bullet prefab has components: Shoot Script, NetworkIdentity, NetworkTransform, Rigidbody 2D

    There is MoveTank Script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class TankMove : NetworkBehaviour
    7. {   public Transform firepoint;
    8.     public GameObject bulletPrefab;
    9.  
    10.     [SerializeField]
    11.     private float TankSpeed = 4;
    12.     private float fireRate = 8;
    13.     private float timeUntilFire = 0;
    14.  
    15.     private void FixedUpdate()
    16.     {
    17.         if (isLocalPlayer)
    18.         {
    19.             // Moving
    20.             transform.Translate(0f, TankSpeed * Time.deltaTime, 0f);
    21.  
    22.             // Setting Mouse Position
    23.             Vector3 mousePosition = Input.mousePosition;
    24.             mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
    25.  
    26.             // Rotating
    27.             Vector2 direction = new Vector2(mousePosition.x - transform.position.x, mousePosition.y - transform.position.y);
    28.             transform.up = direction;
    29.  
    30.             // Shooting
    31.             if (Input.GetButtonDown("Fire1") && Time.time > timeUntilFire)
    32.             {
    33.                 timeUntilFire = Time.time + 1 / fireRate;
    34.                 CmdShoot();
    35.             }
    36.         }
    37.     }
    38.  
    39.     [Command]
    40.     void CmdShoot()
    41.     {
    42.         GameObject bullet = Instantiate(bulletPrefab, firepoint.position, firepoint.rotation);
    43.         NetworkServer.Spawn(bullet);
    44.     }
    45.  
    46.     private void OnMouseOver()
    47.     {
    48.         TankSpeed = 0;
    49.     }
    50.  
    51.     private void OnMouseExit()
    52.     {
    53.         TankSpeed = 4;
    54.     }
    55. }
    56.  
    firepoint is BulletPosition
    bulletPrefab is Bullet Prefab


    There is Shoot Script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Shoot : MonoBehaviour
    6. {
    7.     public float bulletSpeed = 15;
    8.  
    9.     private void FixedUpdate()
    10.     {
    11.         transform.Translate(0f, bulletSpeed * Time.deltaTime, 0f);
    12.     }
    13. }
    14.  
    Please help me,
    Thanks.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you are using the NetworkTransform component, for objects with server authority they are updated from the server to all clients (movement locally on clients is effectively overwritten when receiving the update from the server). For objects with client authority, the client with authority sends the update to the server, and then the server sends the update out to all other clients.

    So take a look at whether you have client or server authority set. Also, since you are using transform.translate, make sure you have the correct setting in the NetworkTransform set for updating transform based movement instead of physics based movement.

    Also, Unet is deprecated. My real recommendation would be to abandon it and move on to a 3rd party network solution which is currently being supported by its developers.
     
  3. RaulStudio

    RaulStudio

    Joined:
    Jun 2, 2023
    Posts:
    1
    Solution to this? Have the same problem