Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiated prefab scale is not displayed correctly on client

I have an explosion represented by a animated prefab. The prefab is instantiated when the user clicks a UI button. When the button is clicked on the client or the host, the prefab is set to localscale of Vector3(0.1,0.1,1) and is displayed correctly on the host, but at orginal scale on the client.

I have a text box showing the scale on both the client and host, which match, but the display of the prefab is different on the host (displayed correctly) and client (displayed incorrectly). Again, the client user can click the button or the host can click the button, and in both cases the display is correct on the host, but not on the client.

Any guesses as to why I can't get the client to display correctly. It appears that the localscale of 0.1,0.1,1 is available on the client as I can show it in a text box, but the prefab is displayed much larger.

This is the instantiation code. Let me know of any other info that would help to diagnose.

go = Instantiate(shockwave, this.transform, false);
NetworkServer.Spawn(go);

UPDATE

This is my updated code based on Lotan's comments. Note that I am calling createshockwave first, which then calls Cmdcreateshockwave because I am not able to call a Command from the UI button.

public void createshockwave()
{
    if(!isLocalPlayer)
    { return; }

    Cmdcreateshockwave();
}

[Command]
public void Cmdcreateshockwave()
{

    //note that this line parents shockwave. The updated instantiation code below does not parent shockwave.
    //go = Instantiate(shockwave, this.transform, false);

    GameObject go = Instantiate(shockwave, this.transform.position, Quaternion.identity) as GameObject;

    NetworkServer.Spawn(go);

    go.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
...
}

Now, the size of the instantiated prefab is the same original scale on both the client and host. Changing the locale scale to other sizes doesn't have any effect.

UPDATE The issue appears to be related to an animator on the prefab. I removed the animator, which scales the prefab, and the scale now matches the parent on both the client and host. Looks like I'll have to manually increase localscale in the update function rather than using an animator.

like image 715
tintyethan Avatar asked Aug 13 '18 20:08

tintyethan


1 Answers

  • NetworkServer.Spawn should be called from a [Command] function

Like this:

[Command]
private void CmdSpawnStuff()
{
    GameObject instance = Instantiate(prefab, 
                                      coords, 
                                      Quaternion.identity) as GameObject;
    NetworkServer.Spawn(instance);
}
  • NetworkServer.Spawn, spawns a default copy of the gameObject, so if you change any properties here, will not be sent to the client, so make the changes AFTER the spawn, and synchronize.

Hope it helps!

like image 159
Lotan Avatar answered Nov 18 '22 14:11

Lotan