Give instantiated object same transform as localPlayer.
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!
Answer by Bogsse · Aug 01, 2016 at 02:22 PM
Try this. Set the object's parent to the player and it should stay with the player. You can make a script on the object's Awake() function so when it spawns it dose this on the server and clients.
Hope this helps.
Your answer
Follow this Question
Related Questions
Keeping an object in a multiplayer scene, when a player joins. 3 Answers
Client's object spawn position and rotation network bug? 0 Answers
Why does NetworkServer.ReplacePlayerForConnection not recognize the instantiated object parameter? 0 Answers
C# Photon Networking - Preventing duplicate GameObjects from spawning on Join. 1 Answer
How to fix snake behavior in Unity3d? 0 Answers