Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Spawning a different prefab depending on whether the character is controlled locally or remotely.

Discussion in 'Multiplayer' started by Lasrin, Nov 21, 2017.

  1. Lasrin

    Lasrin

    Joined:
    Jan 6, 2017
    Posts:
    4
    Hello!

    I'm currently using the HLAPI, as well as the SteamVR library. I have two prefabs: one to be spawned to represent a local user; one to be spawned to represent remote users. The first prefab includes the SteamVR component, so it is only appropriate to be spawned with the local user. The second prefab is just for visually representing where the other users are located.

    Both prefabs are related in the following way - They both have a script from which they inherit called 'UserContext'. The sync variables are all in that base script. I presume that as long as all the variables that are sync'd are in the same base class, there should be no issues synchronizing them, even if they are of different sub-types.

    Could someone please advise on how to achieve this 'locality-determined' prefab spawning with the HLAPI? I'm presuming it requires that I override the OnServerAddPlayer method, but I've tried doing so, and it always just spawns out the prefab that I set for the Spawn Info -> Player Prefab, or nothing at all, which is not what I want.

    Thank you for your time!
     
  2. marcV2g

    marcV2g

    Joined:
    Jan 11, 2016
    Posts:
    115
    They need to be the same prefab since HLAPI used hash codes to find prefabs.

    Currently I have a net prefab that loads a inner prefab depending on if it is local.

    private void Start()
    {
    if (this.isLocalPlayer)
    {
    //load fps model
    }
    else
    {
    //load tps model
    }
    }
     
  3. Lasrin

    Lasrin

    Joined:
    Jan 6, 2017
    Posts:
    4
    I see. I think I have to separate these two types and have the former spawn the latter. Intuitively, this is backwards as this really is more of an is-a relationship rather than a has-a, but it seems to be the idiomatic approach.

    Thanks for your help!