Unity3d Unet multiplayer - only server host can place buildings network aware - unity3d

I am writting an multiplayer game but I am stuck with building system - I mean I've created a code to place blocks it works perfectly fine on host-side (when you run this script on host the block spawn for every client) but it isn't working on client side ( blocks spawn only for local client but not for server and other clients ).
I've seen many simillar problems but no answer was found this day. Maybe you guys could give me a hand.
Here's my code:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Builder : NetworkBehaviour {
public GameObject preview;
public Transform currentPreview;
bool isPreviewing = false;
GameObject buildingPreview;
private NetworkIdentity networkId;
// Use this for initialization
void Start ()
{
networkId = GetComponent<NetworkIdentity>();
}
// Update is called once per frame
void ViewPreview()
{
buildingPreview = Instantiate(preview, transform.position, transform.rotation) as GameObject;
currentPreview = buildingPreview.transform;
isPreviewing = true;
}
void Update ()
{
CmdBuild();
}
void CmdBuild()
{
if (networkId.isLocalPlayer)
{
}
else
{ return; }
if (Input.GetKeyDown(KeyCode.E))
{
if (!isPreviewing)
ViewPreview();
else
{
Destroy(buildingPreview);
isPreviewing = false;
}
}
if (isPreviewing)
{
Preview();
}
}
[Command]
void CmdSpawnBuilding()
{
GameObject buildingPlaced = Instantiate(preview, currentPreview.position, currentPreview.rotation) as GameObject;
NetworkServer.Spawn(buildingPlaced);
}
void Preview()
{
currentPreview.position = transform.position + transform.forward * 3f;
currentPreview.rotation = transform.rotation;
if (Input.GetButtonDown("Fire1"))
{
CmdSpawnBuilding();
isPreviewing = false;
}
}
}
P.S: I have network identity script assigned to prefab I am creating network aware and I have that prefab in my Network Manager as Registred Spawnable Prefabs.

Related

Setting tags on a Unity GameObject with Photon

I am trying to tag two PUN instantiated game objects with "Player1" and "Player2" tags by looking at their PhotonView ViewIDs through an RPC call. I am able to successfully tag the player 1 game object with the player 1 tag, however, no matter what I try, I am unable to set the player2 tag to the player2 object. The code is networked and running on two Oculus Quest headsets. I can start the application on one Quest and it will assign the Player1 tag properly. However, when I start the application on the second Quest, it spawns a player gameobject, but does not tag the object with the Player2 tag even though the player 2 object's PhotonView matches the "2001" value. Below is the code that I am using to spawn in an XROrigin and a networked representation for each player.
using System;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.XR.Interaction.Toolkit;
public class NetworkPlayerSpawner : MonoBehaviourPunCallbacks
{
public GameObject XROriginPrefab;
[HideInInspector]
public GameObject spawnedPlayerPrefab;
private PhotonView pv;
private void Start()
{
pv = GetComponent<PhotonView>();
}
private void Update()
{
// Debug.Log(PhotonNetwork.CurrentRoom.PlayerCount);
}
public override void OnJoinedRoom()
{
base.OnJoinedRoom();
var playerCount = PhotonNetwork.CurrentRoom.PlayerCount;
Debug.Log("The player count is: " + playerCount);
var teleportAreas = GameObject.FindGameObjectsWithTag("Floor");
//playerCount = 2;
if (playerCount == 1)
{
XROriginPrefab = Instantiate(XROriginPrefab, new Vector3(0, 2.36199999f, 3.78999996f),
new Quaternion(0, 0, 0, 1));
spawnedPlayerPrefab = PhotonNetwork.Instantiate("Network Player", transform.position, transform.rotation);
//spawnedPlayerPrefab.tag = "Player1";
foreach (GameObject go in teleportAreas)
{
go.AddComponent<TeleportationArea>();
}
}
else
{
XROriginPrefab = Instantiate(XROriginPrefab, new Vector3(-10.3859997f,1.60699999f,10.6400003f),
new Quaternion(0,0,0,1));
spawnedPlayerPrefab = PhotonNetwork.Instantiate("Network Player", transform.position, transform.rotation);
//spawnedPlayerPrefab.tag = "Player2";
//If teleport breaks again, I uncommented this line, so it should be commented out again. Should allow for teleport in User 2's room.
foreach (GameObject go in teleportAreas)
{
go.AddComponent<TeleportationArea>();
}
}
rpcCallTagAssign();
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
base.OnPlayerEnteredRoom(newPlayer);
Debug.Log("Remote Player Joined!");
rpcCallTagAssign();
}
public override void OnLeftRoom()
{
base.OnLeftRoom();
PhotonNetwork.Destroy(spawnedPlayerPrefab);
}
[PunRPC]
private void tagAssign()
{
if (spawnedPlayerPrefab.GetComponent<PhotonView>().ViewID==1001)
{
spawnedPlayerPrefab.tag = "Player1";
}
if (spawnedPlayerPrefab.GetComponent<PhotonView>().ViewID==2001)
{
spawnedPlayerPrefab.tag = "Player2";
}
}
private void rpcCallTagAssign()
{
pv.RPC("tagAssign", RpcTarget.AllViaServer);
}
}
I am new to networking with Photon, so any help with resolving this issue would be greatly appreciated. Thank you!
The code needs to run on each player (including the copies). The current code can only change the object you have a reference for (spawnedPlayerPrefab). The easiest way is to add an RPC function on the player. That RPC would get called for each instance of that player across the connected clients.
Script On Player.
[PunRPC]
private void AssignTag()
{
if (photonView.ViewID == 1001)
{
gameObject.tag = "Player1";
}
else if (photonView.ViewID == 2001)
{
gameObject.tag = "Player2";
}
}
In NetworkPlayerSpawner
spawnedPlayerPrefab = PhotonNetwork.Instantiate(...);
spawnedPlayerPrefab.GetComponent<PhotonView>().RPC("AssignTag", RpcTarget.AllBufferedViaServer);
The RPC is buffered so future clients entering after you will set your player copy to the correct tag as well (or vice versa).

Stop and Play Particle System on Networked Player in Unity using Mirror

I am trying to play and stop a particle system which is a child of a player prefab. So far I have gotten the host client's thrust particle effect to update across all clients but I haven't been able to figure out a non-host client. I would like it if every time a player presses the thruster button the game would display the thruster effect across all clients and when they stop the particle system stops and other clients would see it as well.
So far my code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class PlayerMovement : NetworkBehaviour
{
public float thrustPower = 300f;
Rigidbody2D m_playerRb;
bool m_thrustOn = false;
public ParticleSystem m_playerPs;
[SyncVar(hook = nameof(SetThrusters))]
bool m_thrusterState = false;
private void Start()
{
m_playerRb = GetComponent<Rigidbody2D>();
m_playerPs.Stop();
}
private void Update()
{
if (isLocalPlayer)
{
MovePlayer();
m_thrusterState = m_thrustOn;
}
}
private void FixedUpdate()
{
if (m_thrustOn)
{
m_playerRb.AddForce(gameObject.transform.up * thrustPower * Time.fixedDeltaTime, ForceMode2D.Force);
}
}
private void SetThrusters(bool oldVal, bool newVal)
{
if (newVal) m_playerPs.Play();
else if (newVal == false) m_playerPs.Stop();
}
private void MovePlayer()
{
m_thrustOn = Input.GetKey(KeyCode.W) ? true : false;
float horizontalInput = Input.GetAxisRaw("Horizontal");
transform.Rotate(new Vector3(0, 0, horizontalInput * -180 * Time.deltaTime));
}
}
I cant really find anything online about Mirror in terms of particle systems. Let me know if you need any more info.
Thanks!
SyncVar always only go in the direction HOST -> CLIENT !
For the other way round you would need to use a Command
like e.g.
[Command]
void CmdSetSetThrusters(bool newVal)
{
// the hook would not be executed on the host => do it manually now
SetThrusters(m_thrusterState, newVal);
// set it on the host -> now via synVar automatically forwarded to others
m_thrusterState = newVal;
}
And then you will have to add a check whether you are the host or not like e.g.
if (isLocalPlayer)
{
MovePlayer();
if(isServer)
{
// afaik this you still have to call manually for yourself
// not sure here how SyncVar Hooks are handled on the host itself
SetThrusters(m_thrusterState, m_thrustOn);
// this is now synced to others
m_thrusterState = m_thrustOn;
}
else
{
// if your not Host send the command
CmdSetSetThrusters(m_thrustOn);
}
}
Personally for this reason for me the SyncVar with Hooks was always a little bit confusing and suspect.
You could as well simply implement both directions yourself using e.g.
[Command]
void CmdSetSetThrusters(bool newVal)
{
// set the value on yourself
SetThrusters(newVal);
// Then directly forward it to all clients
RpcSetThrusters(newVal);
}
[ClientRpc]
private void RpcSetThrusters(bool newValue)
{
// set the value on all clients
SetThrusters(newVal);
}
private void SetThrusters(bool newVal)
{
if (newVal)
{
m_playerPs.Play();
}
else
{
m_playerPs.Stop();
}
// if you are the HOST forward to all clients
if(isServer)
{
RpcSetThrusters(newVal);
}
}
and then
if (isLocalPlayer)
{
MovePlayer();
if(isServer)
{
SetThrusters(m_thrusterState);
}
else
{
// if your not Host send the command
CmdSetSetThrusters(m_thrustOn);
}
}

Unity Game Object Controlled by real light spot on the wall

We have got various controllers developed for moving gameobjects. I have used magnetometer/gyro sensor to move game object using MUVSlide through following code:
using UnityEngine;
using ForestIndieGames.Muvslide;
public class Connection : MonoBehaviour {
private static bool created = false;
private bool newInputAvailable;
private MuvslideConnection muvslideConn;
private void Awake()
{
if (!created)
{
DontDestroyOnLoad(this.gameObject);
created = true;
muvslideConn = new MuvslideConnection();
}
}
private void OnApplicationQuit()
{
if (muvslideConn != null)
muvslideConn.Close();
}
private void Update()
{
if (muvslideConn.GetInputManager().IsNewMotionAvailable())
newInputAvailable = true;
}
public bool IsNewInputAvailable()
{
bool result = newInputAvailable;
newInputAvailable = false;
return result;
}
public Vector3 GetAngles()
{
float[] angles = muvslideConn.GetInputManager().GetOrientationDegrees();
return new Vector3(angles[0], angles[1], angles[2]);
}
}
What I am trying to achieve is to move a gameobject by a real light spot on the wall. The spot is on the wall and fed through a camera. When the spot moves I want game object to follow exactly. The light spot can be a specific color or IR or UV etc.
Any leads please

Network Player Direction in Unity

I am currently building my first multiplayer game in unity and having a little bit of an issue changing/transmitting the players direction over the network.
My current player controls class is this:
using UnityEngine;
using System.Collections;
public class playerControls : MonoBehaviour {
#region
//Vars
//Movements Vars
public float runSpeed;
Rigidbody mybody;
Animator myAnimator;
bool playerDirectionE;
#endregion
// Use this for initialization
void Start () {
mybody = GetComponent<Rigidbody>();
myAnimator = GetComponent<Animator>();
playerDirectionE = true;
mybody.transform.eulerAngles = new Vector3(0, 90, 0);
}
// Update is called once per frame
void Update () {
}
void FixedUpdate()
{
float move = Input.GetAxis("Horizontal");
myAnimator.SetFloat("speed", Mathf.Abs(move)); //To intiate the charecter transition(move)
mybody.velocity = new Vector3(move * runSpeed, mybody.velocity.y, 0); //move charecter along the x axis, and keep y on gravity, not touching the z axis
if(move>0 && !playerDirectionE)
{
Flip();
}
else if(move<0 && playerDirectionE)
{
Flip();
}
}
void Flip()
{
playerDirectionE = !playerDirectionE;
Vector3 theScale = transform.localScale;
theScale.z *= -1;
transform.localScale = theScale;
}
}
I am using the following client information to send commands over the network:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using System.Collections.Generic;
public class PlayerSyncRotation : NetworkBehaviour {
[SyncVar(hook ="OnPlayerRotSynced")]
private float syncPlayerRotation;
[SerializeField]
private Transform playerTransform;
private float lerpRate = 15;
private float lastPlayerRot;
private float threshold = 1;
private List<float> syncPlayerRotList = new List<float>();
private float closeEneough = 0.3f;
[SerializeField]
private bool userHistoricalInterpolation;
[Client]
void OnPlayerRotSynced(float latestPlayerRotation)
{
syncPlayerRotation = latestPlayerRotation;
syncPlayerRotList.Add(syncPlayerRotation);
}
[Command]
void CmdProvideRotationsToServer(float playerRot)
{
syncPlayerRotation = playerRot;
}
[Client]
void transmitRotations()
{
if (isLocalPlayer)
{
if(CheckIfBeyondThreshold(playerTransform.localScale.z, lastPlayerRot)){
lastPlayerRot = playerTransform.localScale.z;
CmdProvideRotationsToServer(lastPlayerRot);
}
}
}
bool CheckIfBeyondThreshold(float rot1, float rot2)
{
if (Mathf.Abs(rot1 - rot2) > threshold)
{
return true;
}
else
{
return false;
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
lerpRotation();
}
void FixedUpdate()
{
transmitRotations();
}
void lerpRotation()
{
if (!isLocalPlayer)
{
HistoricalInterpolation();
}
}
void HistoricalInterpolation(){
if (syncPlayerRotList.Count > 0)
{
LerpPlayerRotation(syncPlayerRotList[0]);
if(Mathf.Abs(playerTransform.localEulerAngles.z - syncPlayerRotList[0]) < closeEneough)
{
syncPlayerRotList.RemoveAt(0);
}
Debug.Log(syncPlayerRotList.Count.ToString() + "syncPlayerRotList Count");
}
}
void LerpPlayerRotation(float rotAngle)
{
Vector3 playerNewRot = new Vector3(0, 0, rotAngle);
playerTransform.rotation = Quaternion.Lerp(playerTransform.rotation, Quaternion.Euler(playerNewRot),lerpRate*Time.deltaTime);
}
}
My rotation looks fine on the client, but over the network on the second client, the rotation is broken and looks very wrong.
I have attached a link to a Webm where you can see the short video snippet of my problem HERE.
Would anyone here have any input as to what I could be doing wrong or how I could fix this issue? any suggestions would be appreciated.
Instead Using your custom code, i will like to say you that use Unet NetworkTransform. It is Unet high level API with different option of customization.
A component to synchronize the position and rotation of networked
objects(more).
Unet HLAPI are open source so you can also get the coding of the networkTransform at bitbucket.

OnCollisionEnter in unity won't call function

I'm a complete unity novice. I want to make a simple scene where you have three lives and you lose a live if you collide with a cube. This is my script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Lives : MonoBehaviour {
public Transform player;
public static int lives;
public Image live1;
public Image live2;
public Image live3;
// Use this for initialization
void Start () {
lives = 3;
live1.enabled = true;
live2.enabled = true;
live3.enabled = true;
}
void Update () {
DisplayOfHearts();
}
public static void Damage() {
lives -= 1;
}
public void OnCollisionEnter(Collision col) {
if (col.gameObject.tag == "cube") {
Lives.Damage();
}
}
public void DisplayOfHearts() {
if (lives == 2) {
live3.enabled = false;
}
else if (lives == 1) {
live2.enabled = false;
}
else if (lives == 0) {
live1.enabled = false;
}
}
}
What happens is the player can't move through the cube but the amount of lives stays three. Is there something I'm missing?
The problem is you have attached the script to a wrong game object. The script and the collider must be attached to the same game object.
Unity methods inside a MonoBehaviour script (such as OnEnable, Update, FixedUpdate, Awake, Start, OnTriggerEnter, OnCollisionStay, etc..) only work for the game object which the script is attached to.
If you attach the script to another game object don't expect any of those to work. Update only works while that game object is active. OnCollisionEnter only works when a collision occurs on a collider which is attached directly to that game object. (it doesn't even work when a child has the collider instead of the actual game object where script is attached to)