1

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);
}
ekhumoro
  • 115,249
  • 20
  • 229
  • 336

2 Answers2

2

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

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.

Wafer
  • 188
  • 3
  • 17
  • The object is not instantiated, must it be in order to work? I just have it in the scene. I have read through the manual but I can't find anything pertaining to updating transforms on non-player objects that are controlled by another device. If you have a direct that would help me that would be great. – Alan-Dean Simonds Aug 07 '16 at 23:10
  • It should work by just spawning it... Im on mobile so sorry for the terrible formatting. Can you try adding an empty gameobject to the scene, add a script to it with this inside: `public GameObject cubePrefab; public override void OnStartServer() { NetworkServer.Spawn(cubePrefab); }` You will need to make a prefab of your cube, and register that prefab to your network manager's spawnable objects, and drag it into the cubePrefab slot in the inspector for the script above. Try interacting with your cube then and get back to me. If you can confirm that that doesn't work I'll be able to help more. – Wafer Aug 07 '16 at 23:46
  • Hi I wont be able to test for a couple of hours, thanks for the help. Just so I have an idea, why must the object be spawned in order to be modified within the game, and will I have to move the script that is on the separate button to the player, or will that object also have to be spawned? – Alan-Dean Simonds Aug 07 '16 at 23:51
  • I updated my answer to include the code you need to spawn the cube on the network, and will add some more information there. – Wafer Aug 08 '16 at 01:35
  • Still weird to me I have to spawn the object in, instead of having in the scene when the game begins. Seems like if I had 50 doors in a scene and had to network spawn them all in, this would be less efficient than having the doors already in the scene. – Alan-Dean Simonds Aug 08 '16 at 01:54
  • I just edited my answer, it turns out your cube should be spawned for you so long as it's in the scene. – Wafer Aug 08 '16 at 01:55