Search Unity

Third Party Mirror and client mouse position

Discussion in 'Multiplayer' started by Credgate, Oct 28, 2020.

  1. Credgate

    Credgate

    Joined:
    May 5, 2016
    Posts:
    13
    Hello,

    I'm having trouble finding my clients mouse position attached is my code...

    https://hatebin.com/yjunejcmnd


    the variables will not change with the mouse position im assuming because the server doesn't know where the clients mouse is. :/
     
  2. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    768
    Try something like this:

    Code (CSharp):
    1. void Update()
    2.     {
    3.  
    4.        if (!isLocalPlayer)
    5.        {
    6.            return;
    7.        }
    8.  
    9.        var worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    10.        var mousePosition = new Vector3(worldPoint.x, worldPoint.y, 0);
    11.        var mouseDirection = (mousePosition - transform.position);
    12.        movement.x = Input.GetAxisRaw("Horizontal");
    13.        movement.y = Input.GetAxisRaw("Vertical");
    14.  
    15.        if (Input.GetKeyDown(KeyCode.Mouse0))
    16.        {
    17.             CmdCast(mouseDirection);
    18.        }
    19.     }
    20.  
    21.     private void FixedUpdate()
    22.     {
    23.         GetComponent<Rigidbody2D>().MovePosition(GetComponent<Rigidbody2D>().position + movement * moveSpeed);
    24.     }
    25.  
    26.     [Command]//sends command to server
    27.     public void CmdCast(Vector3 mouseDirection)
    28.     {
    29.         GameObject temp = Instantiate(bullet, transform.position, transform.rotation);
    30.         temp.GetComponent<Rigidbody2D>().AddForce(mouseDirection.normalized, ForceMode2D.Impulse);
    31.         Destroy(temp, 3);
    32.         NetworkServer.Spawn(temp);
    33.     }
    34.  
    * you don't need worldPoint, mousePosition and mouseDirection to be fields, you can just make them local variables.
    * You can pass the data you need as a parameter to the Command.
    * Data does not synchronize from clients to server, if you want to pass data to the server, you need to pass it as parameters of a command
    * Do you really need to network spawn the bullet? can you just instantiate it in the client and animated without involivng network? The server could still check if you hit something with a ray cast or something
     
  3. Credgate

    Credgate

    Joined:
    May 5, 2016
    Posts:
    13
    * you don't need worldPoint, mousePosition and mouseDirection to be fields, you can just make them local variables.
    * You can pass the data you need as a parameter to the Command.
    * Data does not synchronize from clients to server, if you want to pass data to the server, you need to pass it as parameters of a command
    * Do you really need to network spawn the bullet? can you just instantiate it in the client and animated without involivng network? The server could still check if you hit something with a ray cast or something[/QUOTE]


    you are correct in everything you said. I had them public instead of serializing them just to check if they were changing. The problem ended up being that i had the camera in perspective instead of orthographic in 2d.