1
\$\begingroup\$

When i drag a nonplayer object from host, client can see it, but when move the object from client, host player doesnt see any changes. How can i fix it?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class DragObject : NetworkBehaviour 
{
private Vector3 screenPoint;
private Vector3 offset;

void OnMouseDown()
{
    screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
    offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}

void OnMouseDrag()
{
    Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
    transform.position = cursorPosition;
}}
\$\endgroup\$
0
\$\begingroup\$

(Note: I'm not well versed in Unity, but I do know my way around enough networking APIs to have a reasonable amount of confidence about what's going on here)

You need to replicate the state changes you want from the client to the host. It looks like what's happening is that you're changing the host's state, and because the host has authority to replicate state to other clients, it gets replicated to everyone else. Right now when the client makes a change, it's just changing its local state and no one else in the game knows about that. Networking APIs usually have this concept of "Authority", to have at least some level of security over the game state (I can't just set the opposing players health to 0, etc).

What you want to do, is send a command from the client to the server (what's called a "Command" in Unity terminology), telling it what you want to change, then make that change on the server. The state will then get replicated to everyone, just as it would if the host had made the request itself.

Looking at the Unity networking API docs, it looks like the only way you can make a request like that from a client to the server, is to route it through your local player object. So you'd need to add a Command to your Player to make that change to your object.

See the following pages from Unity's networking docs.

https://docs.unity3d.com/Manual/UNetActions.html https://docs.unity3d.com/Manual/UNetPlayers.html

\$\endgroup\$
  • \$\begingroup\$ Thanks for your clarification. Your post is helped me figure out what i need to do. I couldnt solve my problem yet but i think now i am closer to succeed. \$\endgroup\$ – Koray May 4 '17 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.