Problem with sync direction a bullet that having a force

So currently working on TPS game that a playercan shoot a bullet to direction of mouse position.
But i got problem that direction of bullet is wrong like this

Capture.png

here gif link https://s8.gifyu.com/images/vlc-record-2020-12-06-11h34m13s.gif

The direction of player when move is correct but not while shot. So when shooting the rotation of player rotate to wrong direction then back to right direction after finish shooting.
Here code PlayerController that handle player movement and shooting
void Update()
    {

        if (!photonView.IsMine)
        {
            //gun.SetActive(false);
            return;
        }

        if (ratefire > 0f)
        {
            ratefire = ratefire - Time.deltaTime;
        }

        gunPos = gun.transform.position;
        gunRot = gun.transform.rotation;

        cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(cameraRay, out cameraRayHit))
        {
            if (Input.GetMouseButtonDown(0) && myPV.IsMine) 
            {
                if(ratefire <= 0f)
                {
                    GetComponent<PhotonView>().RPC("ShotGun", RpcTarget.AllBuffered);
                    ratefire = 0.5f;
                }
            }
[PunRPC]

    private void ShotGun()
    {
        Vector3 targetPos = new Vector3(cameraRayHit.point.x, transform.position.y, cameraRayHit.point.z);
        transform.LookAt(targetPos);
        audioSource.PlayOneShot(gunshotSound);
        GameObject _bullet = Instantiate(bullet, gun.transform.position, Quaternion.identity) as GameObject;
        Rigidbody rb_bullet = _bullet.GetComponent<Rigidbody>();
        //rb_bullet.AddForce(gun.transform.forward * 1000f);
        rb_bullet.AddForce(this.transform.forward * 1000f);
        Destroy(_bullet,1f);
    }

    #region IPunObservable implementation

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(this.healthPoint);
            stream.SendNext(this.isGrounded);
            stream.SendNext(this.wasGrounded);
            stream.SendNext(this.gunPos);
            stream.SendNext(this.gunRot);
            stream.SendNext(this.targetPos);
        }
        else
        {
            this.healthPoint = (float)stream.ReceiveNext();
            this.isGrounded = (bool)stream.ReceiveNext();
            this.wasGrounded = (bool)stream.ReceiveNext();
            this.gunPos = (Vector3)stream.ReceiveNext();
            this.gunRot = (Quaternion)stream.ReceiveNext();
            this.targetPos = (Vector3)stream.ReceiveNext();
        }
    }

    #endregion

So first i try to shot bullet to direction forward of Player. Is still wrong. Then i try to using empty object "gun" then send position and rotation with OnPhotonSerializeView is still same. Any idea why and how to fix it? Thank you

Comments

  • I had the same problem I solved by doing this ;

    You should find targetPos before activating ShotGun() function.


    what I am saying is you should pass target postion when you call RPC function with this way everbody knows where is the mouse position.

    like this ;

     
    
     GetComponent<PhotonView>().RPC("ShotGun", RpcTarget.AllBuffered ,targetPos );
    
     private void ShotGun(Vector 3 targetPos)
        {
            transform.LookAt(targetPos);
            audioSource.PlayOneShot(gunshotSound);
            GameObject _bullet = Instantiate(bullet, gun.transform.position, Quaternion.identity) as GameObject;
            Rigidbody rb_bullet = _bullet.GetComponent<Rigidbody>();
            //rb_bullet.AddForce(gun.transform.forward * 1000f);
            rb_bullet.AddForce(this.transform.forward * 1000f);
            Destroy(_bullet,1f);
        }
    




    but unfortunatly this isnt perfect solition because bullets not going same exact position with each players.