Search Unity

Bullet position for each client is different

Discussion in 'Multiplayer' started by harpingseal, Oct 22, 2021.

  1. harpingseal

    harpingseal

    Joined:
    Sep 3, 2020
    Posts:
    57
    So I've got a code for my simple bullet, heres a part of it
    Code (CSharp):
    1.         if (Input.GetMouseButton(0) && Time.time > Nextfire)
    2.         {
    3.             Fire();
    4.             Nextfire = Time.time + Cooldown;
    5.         }
    6.  
    7.     }
    8.     [Command]
    9.     void Fire()
    10.     {
    11.         GameObject projectile = Instantiate(bullet, bulletPos.position, gameObject.transform.rotation);
    12.         NetworkServer.Spawn(projectile);
    13.         var bulletscript = projectile.GetComponent<BulletDamage>();
    14.         var rigid = gameObject.GetComponent<Rigidbody>();
    15.         bulletscript.velocity = rigid.velocity.magnitude + bulletscript.velocity;
    16.     }
    17. }
    And its basicly a modification to mirror tank demod code
    Code (CSharp):
    1.  if (Input.GetKeyDown(shootKey))
    2.             {
    3.                 CmdFire();
    4.             }
    5.         }
    6.  
    7.         // this is called on the server
    8.         [Command]
    9.         void CmdFire()
    10.         {
    11.             GameObject projectile = Instantiate(projectilePrefab, projectileMount.position, transform.rotation);
    12.             NetworkServer.Spawn(projectile);
    13.             RpcOnFire();
    14.         }
    so Why my code,The first one,When the host+client hit fire,the bullet will came out from the client perspective eg. if host is at 0,0,0 and player 1 is at 100,100,100,host shoot the bullet,host will see the bullet being shoot from 0,0,0 and player 1 will see it being shoot from 100,100,100,while the tank demo bullet was fire from the client position.Local player is checked
     
    Last edited: Oct 22, 2021