- Home /
Help with syncing non-player game objects with other players using UNet please?
Hey! So I made this game where you have a given time to spawn in a load of blocks (by hitting an in-game button), picking up those blocks and placing them on top of one another to build a tower. I then started working on the multiplayer aspect of the game. I have successfully synced the players, however the issue comes with the blocks themselves. When a player runs into a button (it's a weird system, I know) a block spawns near the button. The player then runs into the block and the block becomes a child of the player and so it moves with them (some other transformations take place so the block can be seen by the player). The spawning and the picking up of the block can be seen by other players - no problem. However, by pressing 'E' the block is dropped, and as soon as it comes into contact with the ground or another block it freezes its position and rotation and gravity is disabled and it becomes part of the tower. The dropping of the block is, oh so sadly :(, not visible to other players, as far as other players are concerned the player that picked up the block is still carrying the block, regardless as to whether it has been placed or not. Below I have attached the relevant scripts and would love some advice from you guys as to how to proceed in syncing this menace of a GameObject. (Please go easy on me, the code is a mess, this is my first successful-ish attempt at a game so... yeah XD)
Pickup script - The picking up and dropping of the blocks by the player are contained here.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Pickup : NetworkBehaviour {
GameObject Player1;
GameObject ToBeChild;
public bool SomethingCarried;
public bool SomethingDropping;
void Start()
{
Player1 = GameObject.Find("FPSController(Clone)");
}
public void OnCollisionEnter(Collision pickingup)
{
if (pickingup.gameObject.tag == ("Building Block"))
{
ToBeChild = pickingup.gameObject;
carrying();
pickingup.gameObject.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Pickup Attempt");
} else
{
//nothing
}
}
public void dropping()
{
Debug.Log("Drop Attempt");
SomethingDropping = true;
SomethingCarried = false;
ToBeChild.transform.parent = null;
Debug.Log("Dropped!");
ToBeChild.gameObject.GetComponent<Rigidbody>().useGravity = true;
ToBeChild.gameObject.GetComponent<Rigidbody>().freezeRotation = true;
ToBeChild.gameObject.GetComponent<Collider>().enabled = true;
ToBeChild.gameObject.tag = "Placed";
if(ToBeChild.gameObject.tag == ("Placed"))
{
SomethingDropping = false;
}
ToBeChild = null;
}
public void carrying()
{
ToBeChild.transform.parent = Player1.transform;
SomethingCarried = true;
ToBeChild.gameObject.tag = "Carrying";
Debug.Log("Carrying!");
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
dropping();
}
if(SomethingCarried == true)
{
ToBeChild.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
ToBeChild.gameObject.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
ToBeChild.gameObject.GetComponent<Rigidbody>().rotation = Quaternion.identity;
ToBeChild.gameObject.transform.localPosition = Vector3.up / 2;
ToBeChild.gameObject.transform.localPosition = Vector3.forward * 2;
ToBeChild.gameObject.GetComponent<Collider>().enabled = false;
}
if (Input.GetKeyDown(KeyCode.LeftControl))
{
SomethingCarried = false;
ToBeChild.gameObject.GetComponent<Collider>().enabled = true;
ToBeChild.transform.parent = null;
ToBeChild.gameObject.GetComponent<Rigidbody>().drag = 1;
ToBeChild.gameObject.GetComponent<Rigidbody>().useGravity = true;
ToBeChild.gameObject.tag = "Building Block";
}
}
}
Ground Collision script - The sticking of the dropped block to other blocks is done here.
using UnityEngine;
using System.Collections;
public class GroundCollision : MonoBehaviour {
public GameObject ground;
void Start () {
gameObject.GetComponent<GroundCollision>().enabled = false;
}
public void OnCollisionEnter(Collision groundcollision)
{
gameObject.GetComponent<GroundCollision>().enabled = true;
ground = groundcollision.gameObject;
}
void Update () {
if (gameObject.tag == ("Placed"))
{
checkgroundorblock();
} else if (gameObject.tag != ("Placed")){
end();
}
}
void checkgroundorblock()
{
if(ground != GameObject.Find("Player 1"))
{
gameObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
gameObject.GetComponent<Rigidbody>().isKinematic = true;
Physics.IgnoreCollision(ground.GetComponent<Collider>(), GetComponent<Collider>());
ground = null;
end();
}
if (ground != GameObject.Find("Player 2"))
{
gameObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
gameObject.GetComponent<Rigidbody>().isKinematic = true;
Physics.IgnoreCollision(ground.GetComponent<Collider>(), GetComponent<Collider>());
ground = null;
end();
}
if (ground != GameObject.Find("Player 3"))
{
gameObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
gameObject.GetComponent<Rigidbody>().isKinematic = true;
Physics.IgnoreCollision(ground.GetComponent<Collider>(), GetComponent<Collider>());
ground = null;
end();
}
if (ground != GameObject.Find("Player 4"))
{
gameObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
gameObject.GetComponent<Rigidbody>().isKinematic = true;
Physics.IgnoreCollision(ground.GetComponent<Collider>(), GetComponent<Collider>());
ground = null;
end();
}
}
void end()
{
Start();
}
}
Thanks in advance for any help!
Your answer

Follow this Question
Related Questions
How to use NetworkManager.ServerChangeScene ? 1 Answer
Spaw dynamic (unregistered) object on network (UNET) 0 Answers
how do you send commands from objects that dont have "Authortiy" unet 1 Answer
can't switching between weapons in multiplayer game? 0 Answers
Network Transport Layer API does not work with iOS to PC? 1 Answer