Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by AdvsNoob · Jan 08, 2017 at 07:41 AM · unity 5networkingparent-childnetworkplayer

[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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

149 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to disbale a network start position on runtime 0 Answers

Best Practice for an object that is both online and offline using uNET 0 Answers

Get name/variables from previously on server players 0 Answers

How to make per-client camera track a rolling ball in UNET 0 Answers

Can't recreate a match using UNET matchmaker 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges