Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by sebeisdrache · Mar 01, 2015 at 08:56 PM · multiplayerplayernetwork.instantiateflickerjoin

Multiplayer Object positions after player rejoin

Hello,

i have the following setup:

As Server i can Spawn objects with "Network.Instantiate(Resources.Load("Items/Food/TestFood"), new Vector3(240.09f, 7.78f, -1633.47f), Quaternion.identity, 0);"

If the player is connected everything is fine, but if the player leaves and rejoins the server, the Instantiated objects are flickering at thier spawn position.

ervy object that has spawned have the following script (and a networkView observing it):

 using UnityEngine;
 using System.Collections;
 
 public class SimpleNetSync : MonoBehaviour
 {
     private float lastSynchronizationTime = 0f;
     private float syncDelay = 0f;
     private float syncTime = 0f;
     private Vector3 syncStartPosition = Vector3.zero;
     private Vector3 syncEndPosition = Vector3.zero;
     private Quaternion syncStartRotation = Quaternion.identity;
     private Quaternion syncEndRotation = Quaternion.identity;
     private bool gotSync = false;
 
     void Update()
     {
         if(!networkView.isMine){
             SyncedMovement();
         }
     }
 
     private void SyncedMovement()
     {
         if(gotSync == true)
         {
             syncTime += Time.deltaTime;
             rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
             rigidbody.rotation = Quaternion.Slerp(syncStartRotation, syncEndRotation, syncTime / syncDelay);
         }
     }
 
     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
     {
         Vector3 syncPosition = Vector3.zero;
         Quaternion syncRotation = Quaternion.identity;
         Vector3 syncVelocity = Vector3.zero;
         
         if (stream.isWriting)
         {
             syncPosition = rigidbody.position;
             stream.Serialize(ref syncPosition);
             
             syncRotation = rigidbody.rotation;
             stream.Serialize(ref syncRotation);
             
             syncVelocity = rigidbody.velocity;
             stream.Serialize(ref syncVelocity);
         }
         else
         {
             stream.Serialize(ref syncPosition);
             stream.Serialize(ref syncRotation);
             stream.Serialize(ref syncVelocity);
             
             syncTime = 0f;
             syncDelay = Time.time - lastSynchronizationTime;
             lastSynchronizationTime = Time.time;
             
             syncEndPosition = syncPosition + syncVelocity * syncDelay;
             syncStartPosition = rigidbody.position;
             
             syncEndRotation = syncRotation;
             syncStartRotation = rigidbody.rotation;
             
             gotSync = true;
         }
     }
 
 }


all thes objects has a rigidbody for physics.

What can i do to spawn the objects correct for the rejoining player?

I think i need something like "getCurrentDataFormSever()"

edit: I found that if the network view observes the object transform it seams working like expected, but the movement is not smooth.

Comment
Add comment · Show 1
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
avatar image sebeisdrache · Mar 01, 2015 at 10:18 PM 0
Share

i think i have a working solution: Some tesing is requierd

 using UnityEngine;
 using System.Collections;
 
 public class SimpleNetSync : $$anonymous$$onoBehaviour
 {
     private float lastSynchronizationTime = 0f;
     private float syncDelay = 0f;
     private float syncTime = 0f;
     private Vector3 syncStartPosition = Vector3.zero;
     private Vector3 syncEndPosition = Vector3.zero;
     private Quaternion syncStartRotation = Quaternion.identity;
     private Quaternion syncEndRotation = Quaternion.identity;
     private bool gotSync = false;
     private bool firstSync = false;
 
     void FixedUpdate()
     {
         if(!networkView.is$$anonymous$$ine){
             Synced$$anonymous$$ovement();
         }
     }
 
     private void Synced$$anonymous$$ovement()
     {
         if(gotSync == true)
         {
             syncTime += Time.deltaTime;
             if(firstSync)
             {
                 rigidbody.position = syncStartPosition;
                 rigidbody.rotation = syncStartRotation;
             }
             else
             {
                 rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
                 rigidbody.rotation = Quaternion.Slerp(syncStartRotation, syncEndRotation, syncTime / syncDelay);
             }
         }
     }
 
     void OnSerializeNetworkView(BitStream stream, Network$$anonymous$$essageInfo info)
     {
         Vector3 syncPosition = Vector3.zero;
         Quaternion syncRotation = Quaternion.identity;
         Vector3 syncVelocity = Vector3.zero;
         
         if (stream.isWriting)
         {
             syncPosition = rigidbody.position;
             stream.Serialize(ref syncPosition);
             
             syncRotation = rigidbody.rotation;
             stream.Serialize(ref syncRotation);
             
             syncVelocity = rigidbody.velocity;
             stream.Serialize(ref syncVelocity);
         }
         else
         {
             stream.Serialize(ref syncPosition);
             stream.Serialize(ref syncRotation);
             stream.Serialize(ref syncVelocity);
             
             syncTime = 0f;
             syncDelay = Time.time - lastSynchronizationTime;
             lastSynchronizationTime = Time.time;
 
             if(!gotSync)
             {
                 syncEndPosition = syncPosition;
                 syncStartPosition = syncPosition;
                 syncEndRotation = syncRotation;
                 syncStartRotation = syncRotation;
                 firstSync = true;
             }
             else
             {
                 syncEndPosition = syncPosition + syncVelocity * syncDelay;
                 syncStartPosition = rigidbody.position;
                 syncEndRotation = syncRotation;
                 syncStartRotation = rigidbody.rotation;
                 firstSync = false;
             }
 
             
             gotSync = true;
         }
     }
 
 }
 

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Buying an Item from the Shop for One Player 1 Answer

Spawn Player when Room Starts - (Multiplayer) 1 Answer

How to use Network.Instantiate Groups 1 Answer

Displaying players name above in multiplayer 1 Answer

Multiplayer Changing Who "Owns" a Script? 0 Answers


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