Hello,
I have a problem, i can’t seem to find a solution on how to instantiate an object and then “attach” it to the localPlayer.
It works only on host, on clients the object stays at the instantiated position.
I’m kinda new to Unity Networking, please help me!
Here is my code, based on unity’s tutorial.
using UnityEngine;
using UnityEngine.Networking;
public class PlayerController : NetworkBehaviour
{
public GameObject something;
public GameObject theItem;
public Transform player;
void Update()
{
if (!isLocalPlayer)
{
return;
}
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
transform.Rotate(0, x, 0);
transform.Translate(0, 0, z);
if (Input.GetKeyDown(KeyCode.Space))
{
Destroy(theItem);
CmdFire();
}
theItem.transform.position = player.position;
}
[Command]
void CmdFire()
{
theItem = (GameObject)Instantiate (
something,
player.position,
player.rotation);
NetworkServer.Spawn(theItem);
}
}
Thank you so much for your time!