Search Unity

Question Problem with sync direction a bullet that having a force

Discussion in 'Multiplayer' started by dihyanfr32, Dec 6, 2020.

  1. dihyanfr32

    dihyanfr32

    Joined:
    Mar 28, 2020
    Posts:
    3
    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



    Here code PlayerController that handling player movement and shooting

    Code (CSharp):
    1. void Update()
    2.     {
    3.  
    4.         if (!photonView.IsMine)
    5.         {
    6.             //gun.SetActive(false);
    7.             return;
    8.         }
    9.  
    10.         if (ratefire > 0f)
    11.         {
    12.             ratefire = ratefire - Time.deltaTime;
    13.         }
    14.  
    15.         gunPos = gun.transform.position;
    16.         gunRot = gun.transform.rotation;
    17.  
    18.         cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    19.  
    20.         if (Physics.Raycast(cameraRay, out cameraRayHit))
    21.         {
    22.             if (Input.GetMouseButtonDown(0) && myPV.IsMine)
    23.             {
    24.                 if(ratefire <= 0f)
    25.                 {
    26.                     GetComponent<PhotonView>().RPC("ShotGun", RpcTarget.AllBuffered);
    27.                     ratefire = 0.5f;
    28.                 }
    29.             }
    Code (CSharp):
    1. [PunRPC]
    2.  
    3.     private void ShotGun()
    4.     {
    5.         Vector3 targetPos = new Vector3(cameraRayHit.point.x, transform.position.y, cameraRayHit.point.z);
    6.         transform.LookAt(targetPos);
    7.         audioSource.PlayOneShot(gunshotSound);
    8.         GameObject _bullet = Instantiate(bullet, gun.transform.position, Quaternion.identity) as GameObject;
    9.         Rigidbody rb_bullet = _bullet.GetComponent<Rigidbody>();
    10.         //rb_bullet.AddForce(gun.transform.forward * 1000f);
    11.         rb_bullet.AddForce(this.transform.forward * 1000f);
    12.         Destroy(_bullet,1f);
    13.     }
    14.  
    15.     #region IPunObservable implementation
    16.  
    17.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    18.     {
    19.         if (stream.IsWriting)
    20.         {
    21.             stream.SendNext(this.healthPoint);
    22.             stream.SendNext(this.isGrounded);
    23.             stream.SendNext(this.wasGrounded);
    24.             stream.SendNext(this.gunPos);
    25.             stream.SendNext(this.gunRot);
    26.             stream.SendNext(this.targetPos);
    27.         }
    28.         else
    29.         {
    30.             this.healthPoint = (float)stream.ReceiveNext();
    31.             this.isGrounded = (bool)stream.ReceiveNext();
    32.             this.wasGrounded = (bool)stream.ReceiveNext();
    33.             this.gunPos = (Vector3)stream.ReceiveNext();
    34.             this.gunRot = (Quaternion)stream.ReceiveNext();
    35.             this.targetPos = (Vector3)stream.ReceiveNext();
    36.         }
    37.     }
    38.  
    39.     #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