hey not sure scripting is the right topic, but i think the people here have the right brains to answer my question
So I’m building a Customizable player character menu which you get before joining a online game.
I’m not a fan of MonoBehaviour and components so mostly I go for code based (serialized)objects…
hence my used method now is spawning the different model parts and saving the names in my serialized player class, so i can rebuild the whole model loading the right parts from Resources.
no problem there… but now going online trying to let the NetworkManager take care of this seems a pain in the ass…
and so I’m doubting if i should give in for once and do a more prefab centered approached.
i guess that would be:
-making a prefabs where all the customizable model part are on one rig, so the player is not actually building his model from “different parts” but just enabling and disabling mesh renders of these parts on the rig.
so I wonder what the “standard” approach to this kind of thing is in unity? and should i go for the prefab/mesh renderer approach, is this how you would do it?
I have a character editor which players will select prior to joining the server. When they join the server I have it take the settings and send them to the server, the server spawns creates those objects and sets a specific NetworkInstanceId of the parent. When another client connects, it takes that parentId, finds it and attaches it.
where do create the objects? I’m mean what script and which function? like in the NetworkManager’s OnServerAddPLayer? or in another override function…
I should get a better overview as to the order of execution of the available override function and what they do
if you have a good link for this, it’s more than welcome.
I find it weird that for example the player.creates works when in Start() but not if i move it to the end of OnStartLocalPLayer()… so I’m guessing the command and callback aren’t instantly executed orso.
My NetworkPlayer script for now:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class NetworkPlayer : NetworkBehaviour
{
[SyncVar]
public Player player;
Rigidbody rb;
RaycastHit hit;
public override void OnStartLocalPlayer()
{
player = FindObjectOfType<GameManager>().currentPlayer;
TransmitLocalPlayerInstance();
Debug.Log("StartLocal");
}
void Start()
{
if(isLocalPlayer)
{
rb = GetComponent<Rigidbody>();
}
player.CreatePlayer(gameObject);
}
void Update()
{
if(isLocalPlayer)
{
if(Input.GetKeyDown("Fire1"))
{
Attack();
}
}
}
void FixedUpdate()
{
if(isLocalPlayer)
{
rb.MovePosition(transform.position + new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"))*player.playerSpeed*Time.deltaTime);
}
}
[Command]
void CmdUpdatePlayerInstanceToServer(Player p)
{
player = p;
player.CreatePlayer(gameObject);
}
[ClientCallback]
void TransmitLocalPlayerInstance()
{
CmdUpdatePlayerInstanceToServer(player);
}
void Attack()
{
Ray ray = Camera.main.ScreenPointToRay(Vector3(Screen.width/2, Screen.height/2, 5));
if(Physics.Raycast(ray, out hit, player.weapon.range))
{
Debug.Log(hit.collider.name);
}
}
}
When I worked on something like this before, I had customization on the Menu for a weapon and then Spawned the Player with it. What I did was on the customize screen I had an Array of each part of the gun (Barrels, Handles, Scopes, etc). When the Player saved their Weapon I’d save/remember the ID of the selected Array so if I had 5 Barrels and wanted the third I’d save 2. Then on the Player I had the same thing, each Player had every attachment/weapon on them and on Spawn I’d set the chosen part as Active in a for loop.
There are many ways of doing things like this, no way is really right it’s more what you feel comfortable with.
I normally would just serialize the “built character model” data. In your case, this sounds like a list of part ids that make up the completed model.
When a player joins the game, everyone else just needs the list of part ids. They can then rebuild the new player’s model for themselves.
You can also make a serializable struct containing part ids and use Unity’s serialization to sync it. Other players should be able to read the struct, compare it to the character model, then update the character model if needed.