Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Mirror Multiplayer, bullet not moving on all clients.

Discussion in 'Multiplayer' started by NotJoshy_, Nov 27, 2022.

  1. NotJoshy_

    NotJoshy_

    Joined:
    Jan 8, 2022
    Posts:
    3
    Hello! I am working on a FPS Shooter, and I am very new to mirror. My goal here, is when the user presses the left mouse button it spawns a bullet that gets shot from the gun pos.
    The problem is that the bullet does not move in the correct position only on the client. It works for the server, but not the client.

    Here is the code
    Player shooter.cs

    Code (CSharp):
    1. using Mirror;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class PlayerShooter : NetworkBehaviour
    6.  
    7. {
    8.     public float speed = 2500f;
    9.     public GameObject bullet;
    10.     public GameObject robot;
    11.     public Transform gun;
    12.  
    13.     private float delay;
    14.  
    15.     // Update is called once per frame
    16.  
    17.  
    18.     void Start()
    19.     {
    20.          
    21.     }
    22.     void Update()
    23.     {
    24.         if (!isLocalPlayer)
    25.         {
    26.             return;
    27.         }
    28.         //Use the left mouse button to shoot
    29.         if (Input.GetMouseButtonDown(0))
    30.         {
    31.             //Instantiate a bullet at the cameras's position and rotation
    32.             //Then apply a force to the bullet where the player is facing
    33.            
    34.             //Instantiate the bullet
    35.             //Get the transform of the camera
    36.             CmdSpawnBullet();
    37.  
    38.  
    39.         }
    40.  
    41.         if (Input.GetKeyDown(KeyCode.P))
    42.         {
    43.            
    44.             CmdSpawnRobot(transform);
    45.         }
    46.     }
    47.    
    48.  
    49.    
    50.     [Command]
    51.     void CmdSpawnRobot(Transform pos)
    52.     {
    53.         GameObject new_robot = Instantiate(robot, pos.position, pos.rotation);
    54.        
    55.         //Spawn the object to the server, along with the client for the second parameter
    56.         NetworkServer.Spawn(new_robot);
    57.  
    58.  
    59.     }
    60.  
    61.  
    62.    
    63.  
    64.     [Command]
    65.     void CmdSpawnBullet()
    66.     {
    67.         if (bullet != null && gun != null)
    68.         {
    69.             GameObject newBullet = Instantiate(bullet, gun.position, gun.rotation);
    70.             //Add force to the bullet that will boost it in the cameras forward directiond
    71.            
    72.             //Set the bullets tag to "PlayerBullet"
    73.             newBullet.tag = "PlayerBullet";
    74.             //Spawn the bullet on all the clients
    75.             NetworkServer.Spawn(newBullet);
    76.  
    77.             RpcLaunchBullet(newBullet);
    78.  
    79.         }
    80.         else if (bullet == null)
    81.         {
    82.             bullet = FindObjectOfType<Bullet>().gameObject;
    83.             Debug.Log("Bullet whas null :((((");
    84.         }
    85.         else if (gun == null)
    86.         {
    87.             Debug.Log("gun whas null :((((");
    88.         }
    89.  
    90.     }
    91.    
    92.  
    93.     [ClientRpc]
    94.     void RpcLaunchBullet(GameObject bullet)
    95.     {
    96.    
    97.         //Make the bullet launch from the gun with force, on all clients
    98.         bullet.GetComponent<Rigidbody>().AddForce(gun.forward * speed);
    99.     }
    100.  
    101. }
    102.