- Home /
First Person Networking
So I'm teaching myself networking- and I'm using the First Person Controller. It's not ideal, I know, but this is a learning experiment. I can host a game, and have clients join. And here comes the issue:
I can have players moving- but their rotation from the mouselook scripts on the FPController asset doesn't update- so from each character's perspective, everybody is looking one direction. How would I go about synchronising those values, when the networkView component can only monitor one other component?
I'm using an adaptation of the paladinstudios networking tutorial for my networking code, which is below (integrated into the FPSInput Standard Asset).
private var motor : CharacterMotor;
private var controller : CharacterController;
private var lastSynchronizationTime : float = 0.0;
private var syncDelay : float = 0.0f;
private var syncTime : float = 0.0f;
private var syncStartPosition : Vector3 = Vector3.zero;
private var syncEndPosition : Vector3 = Vector3.zero;
// Use this for initialization
//var pcamera : GameObject;
function Awake () {
motor = GetComponent(CharacterMotor);
controller = GetComponent(CharacterController);
}
// Update is called once per frame
function Update () {
if (networkView.isMine) {
InputMovement();
} else {
SyncedMovement();
}
}
function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo) {
var syncPosition : Vector3 = Vector3.zero;
var syncVelocity : Vector3 = Vector3.zero;
if(stream.isWriting) {
syncPosition = transform.position;
stream.Serialize(syncPosition);
syncPosition = controller.velocity;
stream.Serialize(syncVelocity);
}
else {
stream.Serialize(syncPosition);
stream.Serialize(syncVelocity);
syncTime = 0f;
syncDelay = Time.time - lastSynchronizationTime;
lastSynchronizationTime = Time.time;
syncEndPosition = syncPosition + syncVelocity * syncDelay;
syncStartPosition = transform.position;
//syncEndPosition = syncPosition;
}
}
function SyncedMovement() {
syncTime += Time.deltaTime;
transform.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
}
function InputMovement(){
// Get the input vector from kayboard or analog stick
var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
// Dividing by the length is cheaper than normalizing when we already have the length anyway
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
// Make sure the length is no bigger than 1
directionLength = Mathf.Min(1, directionLength);
// Make the input vector more sensitive towards the extremes and less sensitive in the middle
// This makes it easier to control slow speeds when using analog sticks
directionLength = directionLength * directionLength;
// Multiply the normalized direction vector by the modified length
directionVector = directionVector * directionLength;
}
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation * directionVector;
motor.inputJump = Input.GetButton("Jump");
}
// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")
Finally, the players are spawned with a simple network instantiate when they join.
Any ideas on how I'd keep everything (like horizontal mouselook on the player, vertical mouselook on the camera, and movement) all in one place so it all synchronises at once?
did you try it doing this way?
give total controll and everything to player and sending the data only to other players like position and rotation of your movement?
so that objects are kind with no controlls at all they are kinda "dead" but moved by networking
Wait- do you mean I should choose an authoritative server system ins$$anonymous$$d? I'm just looking into that, and it does look promising.
Your answer
Follow this Question
Related Questions
Photon Unity Networking (Viking Demo) Error: Argument is out of range. 1 Answer
Animation with root motion synchronization in multiplayer 1 Answer
Multiplayer client and server aren't talking to each other properly after changing scenes 0 Answers
Check if RPC was received or acknowledged by the client 1 Answer