Search Unity

Question Can't addforce to a rigidbody with ServerRpc

Discussion in 'Netcode for GameObjects' started by zeroject, Aug 9, 2021.

  1. zeroject

    zeroject

    Joined:
    Aug 28, 2020
    Posts:
    2
    Hello, i have trying to make a shooting game where bullets drop over distance, i use addforce to give the bullet som speed. There is no problem with the person hosting but for everyone else their bullets just spawn and falls to the ground. Even the host can see the bullets spawn and fall to the ground. Im using the latest MLAPI and Unity Version 2021.1.3f1

    My code for the spawning the bullet.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using MLAPI;
    5. using MLAPI.Messaging;
    6. using MLAPI.Spawning;
    7.  
    8. public class Shooting : NetworkBehaviour
    9. {
    10.     //DO NOT TOUCH
    11.     private bool isReloading = false;
    12.     //Objects
    13.     public GameObject bullet;
    14.     public GameObject Camera;
    15.     public GameObject yRot;
    16.     //Parametes
    17.     float v_Damage;
    18.     float v_BulletSize;
    19.     int v_MaxAmmo = 5;
    20.     int v_Curr_Ammo;
    21.     float v_FireRate = 0;
    22.     float v_BulletSpeed = 1000f;
    23.     float v_ReloadTimer = 3f;
    24.     float x, y;
    25.     // Start is called before the first frame update
    26.     void Start()
    27.     {
    28.         v_Curr_Ammo = v_MaxAmmo;
    29.         x = transform.rotation.x;
    30.         y = transform.rotation.y;
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         if (IsLocalPlayer)
    37.         {
    38.             x = Camera.transform.rotation.x;
    39.             y = yRot.transform.rotation.y;
    40.  
    41.             if (Input.GetMouseButton(0) && v_FireRate <= 0 && v_Curr_Ammo != 0)
    42.             {
    43.                 ShootServerRpc();
    44.  
    45.                 v_FireRate = .5f;
    46.                 v_Curr_Ammo -= 1;
    47.             }
    48.  
    49.             if (v_FireRate != 0)
    50.             {
    51.                 f_FireRate();
    52.             }
    53.  
    54.             if (v_Curr_Ammo == 0)
    55.             {
    56.                 if (!isReloading)
    57.                 {
    58.                     StartCoroutine(Reloader());
    59.                     isReloading = true;
    60.                 }
    61.             }
    62.         }
    63.     }
    64.  
    65.     void f_FireRate()
    66.     {
    67.         v_FireRate -= Time.deltaTime;
    68.     }
    69.  
    70.     IEnumerator Reloader()
    71.     {
    72.         Debug.Log("Reloading");
    73.         yield return new WaitForSeconds(v_ReloadTimer);
    74.         v_Curr_Ammo = v_MaxAmmo;
    75.         isReloading = false;
    76.     }
    77.  
    78.     [ServerRpc]
    79.     void ShootServerRpc()
    80.     {
    81.         ulong? prefabHash = NetworkSpawnManager.GetPrefabHashFromGenerator("Bullet");
    82.         GameObject instBullet = Instantiate(bullet, transform.position, Quaternion.identity * Quaternion.Euler(x, y, 0f)) as GameObject;
    83.         instBullet.GetComponent<NetworkObject>().SpawnWithOwnership(OwnerClientId);
    84.         Rigidbody instBulletRigidbody = instBullet.GetComponent<Rigidbody>();
    85.         instBulletRigidbody.AddForce(transform.forward * v_BulletSpeed);
    86.     }
    87. }
     
  2. zeroject

    zeroject

    Joined:
    Aug 28, 2020
    Posts:
    2
    Okay found a fix for my own problem. Instead of trying to addforce externally just put the part of the script on the bullet, so my shooting script only has doing with the spawning of the bullet, on that bullet is there a script that keeps track on if it has hit anything and moving the bullet with addforce.
     
    namm3 likes this.