[UNET] Cant get player to parent with boat and sync position
This is what I am basically trying to do. (I will post a slopy version of my code below, slopy because Ive been hacking and slashing trying to make this work for the past 4 hours).
Player spawns in as a captain, when the player loads in I wanted to spawn the ship below him and parent him to the ship. (I got all of that working) however when the ship moves the client side sees the ship move but there position wont be updated to the ship like it is on the host side..
And when the player moves he is snapped back to his original position and once you stop moving he will slowly move in the same direction of the ship..
Sloppy code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class PlayerController : NetworkBehaviour {
[SerializeField] float speed = 6f;
[SerializeField] float cameraDistance = 4f;
[SerializeField] float cameraHeight = 2f;
GameObject ship;
BoatController shipInfo;
//Starter Ship
public GameObject beginnerRaft;
public bool isCaptain = true;
public float bounds;
//Camera movement
Transform mainCamera;
Vector3 cameraOffset;
//Player Movement
public Vector3 playerLocalPosition;
// Use this for initialization
void Start () {
//Spawn ship if captain
if (isCaptain) {
CmdSpawnShip ();
}
if (!isLocalPlayer) {
Destroy (this);
return;
}
cameraOffset = new Vector3(0f,cameraHeight,cameraDistance);
mainCamera = Camera.main.transform;
MoveCamera ();
}
// Update is called once per frame
void Update () {
if (!isClient)
return;
playerLocalPosition = transform.localPosition;
if (Input.GetAxisRaw ("Horizontal") != 0) {
transform.Translate (Vector3.left * Input.GetAxisRaw ("Horizontal") * speed * Time.deltaTime,Space.Self);
}
if (Input.GetAxisRaw ("Vertical") != 0) {
transform.Translate (Vector3.back * Input.GetAxisRaw ("Vertical") * speed * Time.deltaTime,Space.Self);
}
//transform.localPosition = new Vector3 (Mathf.Clamp(transform.localPosition.x,-bounds,bounds), 1.0f, Mathf.Clamp(transform.localPosition.z,-bounds, bounds));
transform.localPosition = new Vector3 (transform.localPosition.x,1,transform.localPosition.z);
MoveCamera ();
}
void MoveCamera(){
mainCamera.position = transform.position;
mainCamera.rotation = transform.rotation;
mainCamera.Translate (cameraOffset);
mainCamera.LookAt (transform);
}
//Spawn Begginner Ship
[Command]
void CmdSpawnShip(){
GameObject go = Instantiate (beginnerRaft,new Vector3(transform.position.x,0,transform.position.z),Quaternion.identity) as GameObject;
ship = go;
transform.SetParent (go.transform);
//transform.SetParent (go.GetComponent<BoatController> ().parentNetID);
transform.position = new Vector3(go.transform.GetChild (0).transform.position.x,1f,go.transform.GetChild (0).transform.position.y);
shipInfo = ship.GetComponent<BoatController> ();
go.GetComponent<BoatController> ().captain = gameObject;
NetworkTransformChild ntc = go.AddComponent<NetworkTransformChild> ();
ntc.enabled = true;
ntc.target = transform;
NetworkServer.Spawn (go);
if (isLocalPlayer) {
RpcSetParent (go);
}
}
[ClientRpc]
void RpcSetParent(GameObject go){
if (!isLocalPlayer)
return;
transform.SetParent (go.transform);
transform.position = new Vector3(go.transform.GetChild (0).transform.position.x,1f,go.transform.GetChild (0).transform.position.y);
}
}
Here is the player: IMG
Here is the ship transform (Used so I can rotate the ship later because I tilt the model for effect): IMG2
Here is the model itself(Attacked to the empty gameobject "ship" shown above): IMG3
Any help or input would be greatly appreciate, I made this game a while ago (All single player) and just started to remake it for multiplayer but dont really understand UNET just as of yet.
Answer by AdvsNoob · Jan 08, 2017 at 10:55 AM
Figured it out, just had to change the parent of the player to the raft model itself, was way to tired to even noticed I didnt do that prior haha.