Question by
SnooksV3 · Jul 21, 2020 at 03:40 AM ·
rigidbodynetworkingmirrorclient-server
Sending Rigidbody Position From Server To Client - Unity Network Using Mirror
I have the following script. I know addForce should be in FixedUpdate, but that's a different question, unless it can be answered...
using UnityEngine;
using System.Collections;
using Mirror;
public class PlayerMovement : NetworkBehaviour {
public Vector3 newPosition; // store position
public Rigidbody rb;
public float jumpForce = 12.0f;
void Start(){
if(!isLocalPlayer){ return; }
// set position
newPosition = rb.transform.position;
}
void Update () {
// if is local player
if(!isLocalPlayer){ return; }
// move up
if(Input.GetAxisRaw("Vertical") > 0){
CmdMovePlayer();
}
// sets position every frame from server
CmdSetPosition();
// moves position every frame
CmdMovePosition();
}
void FixedUpdate () {
}
// run on server
[Command]
public void CmdMovePlayer(){
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
// gets position every frame from server
[Command]
public void CmdSetPosition(){
// position of player on server
newPosition = rb.transform.position;
}
// sets position every frame from server
[Command]
public void CmdMovePosition(){
RpcMovePosition();
}
// send to all clients
[ClientRpc]
private void RpcMovePosition(){
rb.transform.position = newPosition;
}
}
I understand it's probably getting the current position of the rigidbody from the client, but how do I avoid this? Is this even possible?
Thanks
Comment
CmdMovePosition(){
RpcMovePosition(newPosition);
}
[ClientRpc]
public void RpcMovePosition(Vector3 v){
rb.transform.position = v;
}