Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party (Mirror) Client forward axis is always returns the global z axis

Discussion in 'Multiplayer' started by TheOtherUserName, Oct 20, 2021.

  1. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    I am currently trying to implement shooting to my game and am casting a ray to see if the player has hit something.
    For some reason tough the code only works for the host and not the client as the clients forward always returns the global z axis.
    Code (CSharp):
    1. void Update()
    2. {
    3.     if(playerMove == null)
    4.         playerMove = GetComponent<PlayerMovement>();
    5.  
    6.     if (isLocalPlayer)
    7.     {
    8.         if (Input.GetKeyDown(KeyCode.Mouse0))
    9.         {
    10.             Shoot(playerMove.playerCamera.transform.forward);
    11.         }
    12.     }
    13. }
    14.  
    15. [Command]
    16. void Shoot(Vector3 dir)
    17. {
    18.     DamagePlayer(dir);
    19. }
    20.  
    21. [ClientRpc]
    22. void DamagePlayer(Vector3 dir)
    23. {
    24.     RaycastHit hit;
    25.  
    26.     if (Physics.Raycast(transform.position, dir, out hit))
    27.     {
    28.         if (hit.transform.gameObject.tag == "Player")
    29.         {
    30.             hit.transform.GetComponent<Player>().health -= 10;
    31.         }
    32.     }
    33. }
    I do not understand why it does that but I am also new to networking and in the process of learning it so I might just am overseeing something here