Instantiated prefab scale is not displayed correctly on client

北慕城南 提交于 2019-12-23 07:56:16

问题


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.


回答1:


  • 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!




回答2:


make sure the scale of both parent objects match in order to make sure they have the same local scale



来源:https://stackoverflow.com/questions/51830401/instantiated-prefab-scale-is-not-displayed-correctly-on-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!