How do I network movement with photon

97 Views Asked by At

not for player movement but I need to network the AI's movement

well I haven't really tried anything because idk how to network movement

1

There are 1 best solutions below

0
On

Basically you can use one of three approaches.

Easy mode

Add PhotonView and SimpleMode2D to your player

public SimpleMode2D : Monobehaviour {

[SerializeField] private PhotonView view;
[SerializeField] private float speed;

private void Update() {
    if(!view.IsMine) return;
    float h = Input.GetAxisRaw(“Horizontal”);
    float v = Input.GetAxisRaw(“Vertical”);

    gameObject.transform.position = new Vector2 (transform.position.x + (h * speed), transform.position.y + (v * speed));
}

RPCMode

You will still need to add PhotonView and RPCPlayer and to your player, but this time your movement control will be separately

public class RPCPlayer : Monobehaviour {

[PunRPC]
private void RPCMove(Vector2 newPosition){
transform.position = newPosition;
}

}
public RPCMode2D : Monobehaviour {

[SerializeField] private PhotonView player;
[SerializeField] private float speed;

private void Update() {
    if(!player.IsMine) return;
    float h = Input.GetAxisRaw(“Horizontal”);
    float v = Input.GetAxisRaw(“Vertical”);
    var position = player.transform.position;
    player.RPC("RPCMove", new Vector2 (position.x + (h * speed), position.y + (v * speed)));
}

RaiseEvent Mode

Here you will need to create player instantiation/deletion and manipulation. I'm using this method because it more flexible but more complex. You can refer to documentation. Sorry here without code for now, I'll update this comment if you get interested in RaiseEvent usage.