#Mouse Movement

1 messages · Page 1 of 1 (latest)

limber oasis
#

The server does not know what the client mouse position is. The easiest way for this would be to use client network transform and then change the ship ownership with a Server RPC

#

Or you could also just send the mouse position in your existing RPCs

remote patrol
limber oasis
remote patrol
limber oasis
remote patrol
limber oasis
remote patrol
#

alright ill try the vector3 solution and see if that does anything mind if i ping you again later when im done?

limber oasis
#

no prob

remote patrol
# limber oasis no prob

i tried putting vector3 in the params but it says it needs an identifier aswell but im not quite sure what to put in as the identifier

limber oasis
#

yea. gotta give the variable a name

remote patrol
#

i assumed that was the case but it still gives me an error saying that theres no argument that corresponds

limber oasis
#

You have to pass the variable in when you call it too

remote patrol
#

oh lol that was the problem thx

#

the client is still not even able to click the ship mhh

#

im gonna try the other method

remote patrol
# limber oasis The server does not know what the client mouse position is. The easiest way for ...
using Unity.Netcode;

public class ShipManagerScript : NetworkBehaviour
{
    public GameObject myShip;
    public GameObject myHitbox;
    public bool isShipFollowingMouse = false;

    // Update is called once per frame
    void Update()
    {
         if (Input.GetMouseButtonDown(0))
         {
             MoveShipServerRpc();
         }
       

        if (isShipFollowingMouse)
        {
            FollowMouseServerRpc();
        }
    }
    
    [ServerRpc(RequireOwnership = false)]
    public void MoveShipServerRpc()
    {
        Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        mousePosition.z = 0;

        Collider2D hitboxCollider = myHitbox.GetComponent<Collider2D>();
        if (hitboxCollider.bounds.Contains(mousePosition))
        {
            isShipFollowingMouse = !isShipFollowingMouse;

            Debug.Log("Ship clicked. isShipFollowingMouse: " + isShipFollowingMouse);
            GetComponent<NetworkObject>().ChangeOwnership(NetworkManager.Singleton.LocalClientId);
        }
    }

    [ServerRpc(RequireOwnership = false)]
    public void FollowMouseServerRpc()
    {
        Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        mousePosition.z = 0;

        myShip.transform.position = new Vector3(mousePosition.x, mousePosition.y, myShip.transform.position.z);
    }
}```
am i missing something? it gives out this error which ive never seen before KeyNotFoundException: The given key 'ShipManagerScript' was not present in the dictionary.
limber oasis
#

You'll need to make sure that this is on a Network Object and that its been Spawned

remote patrol
#

wait i changed it from a server to a client rpc and i getting somewhere (or maybe not) now it changes the bool and it gives the debug message when its being clicked using the client but it says only server can change ownership

#

does that mean it has to be a serverrpc