#Mouse Movement
1 messages · Page 1 of 1 (latest)
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
is that not what im doing whit this?
[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);
}
}
no nothing is getting sent in the RPC. Only what is in the parameters get sent.
also wdym change the ownership, youre not the first to tell me this but i looked it up and couldnt find anything
Ownership is what gives clients the permission to change network variable and client network transforms if you are using them
https://docs-multiplayer.unity3d.com/netcode/current/basics/networkobject/
Netcode for GameObjects' high level components, the RPC system, object spawning, and NetworkVariables all rely on there being at least two Netcode components added to a GameObject:
oh alright i get you so just sending like a Vector3 in the params would work
right
The docs for RPCs are here
https://docs-multiplayer.unity3d.com/netcode/current/advanced-topics/message-system/rpc/
Any process can communicate with any other process by sending an RPC. Starting in version 1.8, the Rpc attribute encompasses Server to Client Rpcs, Client to Server Rpcs, and Client to Client Rpcs.
thx
alright ill try the vector3 solution and see if that does anything mind if i ping you again later when im done?
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
yea. gotta give the variable a name
i assumed that was the case but it still gives me an error saying that theres no argument that corresponds
You have to pass the variable in when you call it too
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
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.
You'll need to make sure that this is on a Network Object and that its been Spawned