Question by
cdeneau · Jan 29, 2017 at 09:36 AM ·
networkingmultiplayerserverclient
How to allow All clients to move blocks?
So I am making a game were there are two players that can move blocks to get to a power up. Spawning the blocks this is attached to an empty gameobject and the network identity is server only
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class BlockSpawner : NetworkBehaviour {
public GameObject blockPrefab;
public GameObject blockPrefab2;
public int blockX;
public int blockY;
[SerializeField]
public Material Material1;
[SerializeField]
public Material Material2;
public override void OnStartServer()
{
bool flag = true;
for (int i = 0; i < blockX; i += 1)
{
for (int j = 0; j < blockY; j += 2)
{
GameObject cube;
if (flag)
{
cube = Instantiate(blockPrefab) as GameObject;
cube.transform.position = new Vector3(j, 0.2f, i);
}
else
{
cube = Instantiate(blockPrefab2) as GameObject;
cube.transform.position = new Vector3(j + 1, 0.2f, i);
}
cube.transform.localScale = new Vector3(1, 1, 1);
NetworkServer.Spawn(cube);
Debug.Log (cube.GetComponent<NetworkIdentity>().hasAuthority);
}
if (flag)
{
flag = false;
}
else
{
flag = true;
}
}
}
}
This is my movement for the blocks right now only the client that is also the server can move the blocks and i get warnings about no authority to call command functions on the client that isn't the server
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class RotateBlock : NetworkBehaviour {
public int countx = 1;
public int countz = 0;
private Vector3 screenPoint;
private Vector3 offset;
private Vector3 v3Pos;
private int threshold = 9;
int direction = -1;
[SyncVar (hook ="OnPosChange")]
public Transform syncPos = null;
//[SyncVar(hook = "OnPositionChange")]
//public Vector3 currentPos;
public void OnPosChange(Transform pos)
{
Debug.Log("OnPosChange called");
syncPos.position = pos.position;
}
public void OnMouseDown()
{
v3Pos = Input.mousePosition;
}
public Texture2D cursorTexture;
CursorMode cursorMode = CursorMode.Auto;
Vector2 hotSpot = Vector2.zero;
public void OnMouseDrag()
{
Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);
var v3 = Input.mousePosition - v3Pos;
v3.Normalize();
var f = Vector3.Dot(v3, Vector3.up);
if (Vector3.Distance(v3Pos, Input.mousePosition) < threshold) {
//Debug.Log("No movement");
return;
}
if (f >= 0.5) {
//Debug.Log("Up");
direction = 1;
}
else if (f <= -0.5) {
//Debug.Log("Down");
direction = 2;
}
else {
f = Vector3.Dot(v3, Vector3.right);
if (f >= 0.5) {
//Debug.Log("Right");
direction = 3;
}
else {
//Debug.Log("Left");
direction = 4;
}
}
Debug.Log (direction);
v3Pos = Input.mousePosition;
}
public void OnMouseUp() {
if (direction == 1) {
CmdMoveBlock(0,1);
} else if (direction == 2) {
CmdMoveBlock(0,-1);
} else if (direction == 3) {
CmdMoveBlock(1,0);
} else if (direction == 4) {
CmdMoveBlock(-1,0);
}
}
public void CmdMoveBlock(int countx, int countz) {
Debug.Log("Called move block");
int tempX = countx;
int tempY = countz;
Vector3 CubeRight = new Vector3 (transform.position.x+0.5f, transform.position.y, transform.position.z);
Vector3 CubeLeft = new Vector3 (transform.position.x-0.5f, transform.position.y, transform.position.z);
Vector3 CubeTop = new Vector3 (transform.position.x, transform.position.y, transform.position.z+0.5f);
Vector3 CubeBottom = new Vector3 (transform.position.x, transform.position.y, transform.position.z-0.5f);
transform.Translate (countx, 0, countz);
GetComponent<SyncPosition>().pos = transform.position;
syncPos = transform;
//GetComponent<SyncPosition>().Cmd_ProvidePositionToServer(temp);
}
}
So I'm just confused on what to do and what else that I can do. Thanks for the help.
Comment