- Home /
Network. How to get progress of moving platform on client login?
Basicly after i start server and spawn all my moving platforms, if client is not logged in and he logs in abit later after spawning moving platforms, then each player has his own moving platforms, server's is farther in progress and they are not synced at all. That also causes dissapearing for each other, while on moving platform. Huge help would be, if anyone would help me understand,how to get that progress, where is moving platform currently when i login with client, when all moving platforms were already spawned like 5 seconds before me. So when i login there wont be new platform for me from beginning, but already in progress of server's platform :) Thx in advance.
Answer by rob5300 · Dec 18, 2014 at 03:30 PM
This should be solved by placing NetworkViews on each of the platforms, and telling the component to observe the transform component (which is done by default).
Now you would only want the server to move the platforms, and the network views make sure that the position is updated on all clients. This can be achieved by checking the bool networkView.isMine. This is true if this is the client that spawned the platforms (only works if Network.Instantiate is used).
This should fix the problem, or alternatively you can use a RPC to send the current position of the platform to the connecting client, and use this data (Vector3) to synchronize them. OnSerializeNetworkView could also help: http://docs.unity3d.com/ScriptReference/Network.OnSerializeNetworkView.html
Oh so basicly its simpler for me to change whole script? Since i use a script with Original and Destination positions, and the train just rides back and forward with pauses.
$$anonymous$$ost likely yes, try out having a single script that manages this, and of course design it with multiplayer in $$anonymous$$d and how you wish to make it work over the network. If you need help, just ask or post code you have made if you have problems.
I use network instantiate for spawning train like you told me to, and it also has the networkview. I think the problem is in this code:
using UnityEngine;
using System.Collections;
public class Train : $$anonymous$$onoBehaviour {
public AudioClip trainAudio;
public GameObject player;
public GameObject train;
public Transform destinationSpot;
public Transform originSpot;
public float speed = 5.0f;
public float pauseDuration = 0f;
private float platformTimer = 0.0f;
public bool Switch = false;
private AudioSource audioSource;
void Start () {
audioSource = GetComponent<AudioSource>();
audioSource.enabled = false;
}
void OnTriggerStay(Collider other){
if(other.tag == "Player"){
other.transform.parent = train.transform;
}
}
void OnTriggerExit(Collider other){
if(other.tag == "Player"){
other.transform.parent = null;
}
}
void FixedUpdate()
{
// For these 2 if statements, it's checking the position of the platform.
// If it's at the destination spot, it sets Switch to true.
if(transform.position == destinationSpot.position && Switch == false)
{
Switch = true;
platformTimer = 0.0f;
audioSource.enabled = false;
}
if (transform.position == originSpot.position && Switch == true)
{
Switch = false;
platformTimer = 0.0f;
audioSource.enabled = false;
}
// If Switch becomes true, it tells the platform to move to its Origin.
if(Switch)
{
// If the platformTimer is more than the pauseDuration then start
// moving the platform else make the platformTImer count up.
if(platformTimer >= pauseDuration)
{
audioSource.enabled = true;
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, originSpot.position, speed * Time.deltaTime);
}else
{
platformTimer += Time.deltaTime;
}
}
else
{
// If Switch is false, it tells the platform to move to the destination.
// If the platformTimer is more than the pauseDuration then start
// moving the platform else make the platformTImer count up.
if(platformTimer >= pauseDuration)
{
audioSource.enabled = true;
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, destinationSpot.position, speed * Time.deltaTime);
} else {
platformTimer += Time.deltaTime;
}
}
}
}
That's complete train code, i quess i should add rpc to movement functions like you suggested(Correct me if im wrong) Then it should work as a charm :) Altho,if it wont,i can use alternative methods for whole script. Thx for your anwser :)
I think the best way would be to only have the server move the platforms, as they have networkviews, the connected clients trains will be in the same position as on the server, as they are synchronized. This is best as we don't have to mess with the code as much to get them in the same positions when we join the server.
From what i understand, to do this you can simply put this in start
if(!networkView.is$$anonymous$$ine){
this.enabled = false;//Should work, never disabled a monobehaviour in the same script before.
}
//This should just disable the script on the clients
//as they don't own this, it isn't not theirs.
//This will then esure that the movemement to the trains is purely managed by the server, and the clients just have theirs updated.
}
I hope this makes sense, if not, have a look at Networkviews, and how they work, or ask again :)
Yeah, i know how they work :) I think i understoon everything. Gonna try it tomorrow. Thx again :) Here's a sneak peak to my project: http://piclair.com/hyeds
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Handle device offline status 0 Answers
How to make something happen when server deployment fails. 0 Answers
PhotonView question 1 Answer
Android and PHP high scores 0 Answers