1
votes

I have a cube in my game that lerps between its initial state and a larger state while looking at another object. This works fine in single player, but when I bring it over to multiplayer I can't find the right combination of options to get it to update on both clients (one being host). Each player can activate their own cube still but is not represented on the other machine. The script is on the button which has a network identity and it accesses the cube which also has a network identity and a network transform.

Single player code reference:

void Update () {

    if (Camera.main != null) {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit)) {
            if (hit.collider.gameObject == gameObject && hit.distance < 5) {
                PlatformScale();
            } else {
                PlatformReset();
            }
        }
    }
}

void PlatformScale () {
    platform.transform.localScale = Vector3.Lerp (platform.transform.localScale, platformScale, 3f * Time.deltaTime);
}
void PlatformReset () {
    platform.transform.localScale = Vector3.Lerp (platform.transform.localScale, platformStartingScale, 3f * Time.deltaTime);
}
2

2 Answers

2
votes

So after much searching I found out that you cannot sync localScale in Unet, it is an outstanding bug. I have changed my code to use position instead of local scale. Thanks for all the help.

1
votes

Just in case you haven't already read through it, I highly suggest going through the Unity Networking Manual

I suggest using [Command] or [ClientRpc] (depending on which objects are on the server and which are local) functions to lerp the scales.

Although, you may not need them since you have network transforms. Make sure you have properly spawned/instantiated the cube on the network.

Edit: To spawn the cube on the network...

using UnityEngine;
using UnityEngine.Networking;

public class CubeStuff : NetworkBehaviour {

    public GameObject cubePrefab;
    public GameObject cube;
    public override void OnStartServer() {
        GameObject cubeSpawn = (GameObject)Instantiate(cubePrefab, transform.position, transform.rotation);
        NetworkServer.Spawn(cubePrefab);
    }
}

HOWEVER: According to the documentation, you don't need to network spawn the object so long as it's in the scene as this is handled for you - so your problem lies somewhere else.

I don't have enough information to really know what you've got setup. Your buttons and UI can be an object in the scene, just make sure they have Local Player Authority checked and that their OnClick() references are calling [Command] functions.