Search Unity

A way to sync shooting while rotating player.

Discussion in 'Multiplayer' started by nbg_yalta, Sep 26, 2016.

  1. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    Hello, in my game player rotation is done by 90 degrees per click, so when player is facing left and press down key, it will rotate to face down. Also player has an ability for shooting while rotation happens, and at this point I've got a problem with wrong bullet direction synchronization.
    Here is some screenshots:


    I guess this might happen because of rotation interpolation, but as you can see the difference is huge and I have no idea how to fix this.
    Shooting code part:
    Code (csharp):
    1.  
    2. void Update ()
    3.     {
    4.         if (Input.GetMouseButtonUp(0))
    5.         {
    6.                 if (isServer)
    7.                     RpcShoot();
    8.                 else
    9.                     CmdShoot();
    10.         }
    11.     }
    12.  
    13.     [Command]
    14.     void CmdShoot()
    15.     {
    16.         RpcShoot();
    17.     }
    18.  
    19.     [ClientRpc]
    20.     void RpcShoot()
    21.     {
    22.         Vector3 pos = thisT.position + thisT.up * bulletSpawnOffset;
    23.         Quaternion rot = thisT.rotation;
    24.         Rigidbody2D bulletObj = Instantiate(bullet, pos, rot) as Rigidbody2D;
    25.         bulletObj.AddForce(bulletObj.transform.up * 350);
    26.     }
    27.  
    Hope for your help!
     
    Last edited: Sep 26, 2016