Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Simple question: let client move game object.

Discussion in 'Multiplayer' started by PirateSixer, Jun 28, 2023.

  1. PirateSixer

    PirateSixer

    Joined:
    Nov 22, 2022
    Posts:
    2
    This is what I want to happen: I have randomly spawned objects. I want each player to be able to interact with the objects by clicking on them and then clicking somewhere else to throw them.

    I have all of that working for several days now in single player. I'm using Raycasts for detection. I have zero issues.

    The problem is multiplayer. I cannot get my client to interact at all. The host works fine, but the client will not interact with anything that has a NetworkTransform on it.

    I have tried ClientRpc, ServerRpc, sending lists, sending everything, spawning and despawning things over and over, clientnetworktransforms, so on.

    I figured I would get the answer eventually, but I have not. For something so seemingly simple it is driving me up a wall. As I was writing this post, I thought I should try moving the player object (which is currently mostly an empty object), and yes, it took two seconds, put a script on it, just worked. Why can't I move other objects like that as well?

    Short version: How the hell do I move objects on a client with raycast?
     
  2. Lukeesta

    Lukeesta

    Joined:
    Jan 7, 2016
    Posts:
    77
    Either:

    - Client authoritative movement: Use ClientNetworkTransform > check for IsOwner > do the raycast and move
    - Server authoritative movment: Use NetworkTransform > check for IsOwner > do raycast > send result position of raycast to server using a ServerRpc > move on server
     
  3. PirateSixer

    PirateSixer

    Joined:
    Nov 22, 2022
    Posts:
    2
    I understand the concept, but it is not working for me, and that is where the issue lies.

    I have a script that checks if a user is the host, and if so spawns a pair of objects.

    Code (CSharp):
    1. if (IsServer && IsLocalPlayer)
    2.                 {
    3.                     var purple = Instantiate(pieceSpawn.CreatePiece(), new Vector3(7.5f, .5f, 0f), Quaternion.identity);
    4.                     var blue = Instantiate(pieceSpawn.CreatePieceDown(), new Vector3(7.5f, 7.5f, 0f), Quaternion.identity);
    5.                  
    6.                     purple.GetComponent<NetworkObject>().Spawn();
    7.                     blue.GetComponent<NetworkObject>().Spawn();
    8.  
    9.                 }
    Then I do a pair of raycasts to select one of those objects, and if have one selected, move it to the new point.

    Code (CSharp):
    1.   if (Input.GetMouseButtonDown(0))
    2.         {
    3.             hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    4.  
    5.             if (hit.collider != null && !hit.collider.CompareTag("Board"))
    6.             {
    7.          
    8.                 selected = hit.collider.gameObject;
    9.             }
    10.             else if (hit.collider != null && selected != null && hit.collider.CompareTag("Board"))
    11.             {
    12.              
    13.                 {
    14.                     selected.transform.position = hit.transform.position;
    15.              
    16.                 }
    17.      
    18.             }
    19.  
    20.         }
    The movement does not work on a client. It works perfectly fine on a host, but no matter what I do I cannot get the movement to work. I have used networktransforms, clientnetworktransforms, (isowner), [serverrpc], [serverrpc(requireownership = false)], a [serverrpc] that calls a [clientrpc]. I have also tried different ways to movement, from translate to otherwise, but I don't think that is the problem, since even in edit mode, if I try to manually edit the values, it simply does not work.
     
    Last edited: Jun 29, 2023