Network Soccer Ball Position jitter, sending transform position late?
Player class
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class SimplePlayer: NetworkBehaviour {
//public bool hasball = false;
public GameObject ball;
void Start(){
ball = GameObject.Find ("Ball");
}
void FixedUpdate () {
if (!isLocalPlayer) {
return;
}
var v = Input.GetAxis ("Vertical");
var h = Input.GetAxis ("Horizontal");
var dir = new Vector3 (h, 0, v);
var sdir = dir * 0.5f;
gameObject.GetComponent<CharacterController> ().Move ((sdir));
ReleaseBall ();
//sync ball
SimpleBall ballscript = ball.GetComponent<SimpleBall> ();
if (ballscript.onwer == this.gameObject) {
if (isServer) {
RpcUpdateBall (transform.position + transform.forward * 2f);
} else {
CmdSendBallPos (transform.position + transform.forward * 2f);
}
}
}
void ReleaseBall(){
if(Input.GetButtonDown("Shoot")){
SimpleBall ballscript = ball.GetComponent<SimpleBall> ();
if (ballscript.onwer == this.gameObject) {
CmdSendBallOwner(ball.gameObject);
}
}
}
void OnControllerColliderHit(ControllerColliderHit col){
if (!isLocalPlayer) {
return;
}
if (col.gameObject == ball) {
CmdSendBallOwner (this.gameObject);
}
}
[Command]
void CmdSendBallOwner(GameObject owner){
RpcRecieveOnwer(owner);
}
[ClientRpc]
void RpcRecieveOnwer(GameObject onwer){
if(isServer){
SimpleBall ballscript = ball.GetComponent<SimpleBall> ();
ballscript.onwer = onwer;
}
}
[Command]
void CmdSendBallPos(Vector3 pos){
RpcUpdateBall (pos);
}
[ClientRpc]
void RpcUpdateBall(Vector3 pos){
if (isServer) {
ball.transform.position = pos;
}
}
}
Ball Class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class SimpleBall : NetworkBehaviour {
[SyncVar] public GameObject onwer;
void Start () {
onwer = this.gameObject;
}
}
So there is jitter whenever a client has the ball, how do i fix that, i tried many ways to program this ball and everything jitters.
Maybe i will have to program a fake ball and prefab a projectile whenever a player shoots it.
Comment