- Home /
Question by
_samuel12345 · May 27, 2018 at 05:10 PM ·
movementnetworkingnetworkmovement script
How to smoothen out ball movement using Photon
Hi, I am making an online 2D soccer game using Photon and need help getting the ball's movement to be smoother. I have already set up the player movement but when the ball moves it is very glitchy and jumps around a lot. I have the following code on the ball:
public class ballMoveNetworking : MonoBehaviour {
Vector3 targetPos;
PhotonView PhotonView;
// Use this for initialization
void Start () {
PhotonView = GetComponent<PhotonView> ();
PhotonNetwork.sendRate = 60;
PhotonNetwork.sendRateOnSerialize = 30;
}
// Update is called once per frame
void Update () {
if (!PhotonView.isMine) {
smoothMove ();
}
}
private void smoothMove(){
transform.position = Vector3.Lerp(transform.position, targetPos, 0.25f );
}
private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if (stream.isWriting) {
stream.SendNext (transform.position);
} else {
targetPos = (Vector3)stream.ReceiveNext ();
}
}
}
This script is in the 'Observed Components' option in a Photon View Component on the ball. Thanks in advance.
Comment
Best Answer
Answer by Unified2000 · May 28, 2018 at 04:15 PM
transform.position = Vector3.Lerp(transform.position, targetPos, 5*Time.deltaTime);
BTW - reduce the 5*TIme.deltaTime if necessary. It all depends on the scale of your game world.