msg/s way to high

Hello, i have a problem with my msg/s rate. The messages a second go up to the 210s with 2 players and all the way up to 975 with 3 players, but i set the msg/s rate to 5. Here is the script i use to send the position and velocity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DataSync : Photon.MonoBehaviour
{
    public Vector3 TrueLocation;
    public PhotonView PhotonView;
    public Rigidbody2D rb;
    

    void Start()
    {
        PhotonNetwork.sendRate = 5;
        PhotonNetwork.sendRateOnSerialize = 5;
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {

        //we are reicieving data
        if (stream.isReading)
        {
                //receive the next data from the stream and set it to the truLoc varible
                if(!PhotonView.isMine){//do we own this photonView?????
                this.TrueLocation = (Vector3)stream.ReceiveNext();//the stream send data types of "object" we must typecast the data into a Vector3 format
                this.rb.velocity = (Vector2)stream.ReceiveNext();
               }
        }
        //we need to send our data
        else
        {
                //send our posistion in the data stream
                if(PhotonView.isMine)
                {
                    stream.SendNext(transform.position);
                    stream.SendNext(rb.velocity);
                }
        }
    }
}

And here is what i use to recieve the data
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PositionSmoothing : Photon.MonoBehaviour
{

    public Vector3 TrueLocation;
    public PhotonView PhotonView;
    public Rigidbody2D rb;
    private bool PositionSmoothingAllowed;
    private bool Teleporting;

    void Start()
    {
        PhotonNetwork.sendRate = 5;
        PhotonNetwork.sendRateOnSerialize = 5;
        TrueLocation = GetComponent<DataSync>().TrueLocation;
        rb = GetComponent<DataSync>().rb;
    }

    void Awake()
    {
        StartCoroutine(AwakeTimer());
    }

    void Update()
    {
        TrueLocation = GetComponent<DataSync>().TrueLocation;
        rb = GetComponent<DataSync>().rb;

        if(!PhotonView.isMine && PositionSmoothingAllowed == false || !PhotonView.isMine && Teleporting == true)
        {
            transform.position = TrueLocation;
        }

        if(!PhotonView.isMine && PositionSmoothingAllowed == true && Teleporting == false)
        {
            transform.position = Vector3.Lerp(transform.position, TrueLocation, Time.deltaTime * 6);
        }
        
        if(PhotonView.isMine && Input.GetKeyDown(KeyCode.LeftAlt) || PhotonView.isMine && Input.GetKeyDown(KeyCode.RightAlt))
        {   
            StartCoroutine(TeleportingTimer());
            transform.position = new Vector3(1.0f, 1.0f, 0.0f);
            TrueLocation = new Vector3(1.0f, 1.0f, 0.0f);
        }

        if(!PhotonView.isMine)
        {
            AnimDirection();
        }
        
    }

    void AnimDirection()
    {
        if(!PhotonView.isMine && rb.velocity.x > 0)
        {
            GetComponent<PlayerController>().AnimDirection = 1;
        }
        if(!PhotonView.isMine && rb.velocity.x < 0)
        {
            GetComponent<PlayerController>().AnimDirection = -1;
        }
        if(!PhotonView.isMine && rb.velocity.x == 0)
        {
            GetComponent<PlayerController>().AnimDirection = 0;
        }
    }
    IEnumerator AwakeTimer()
    {
        PositionSmoothingAllowed = false;
        yield return new WaitForSeconds(1);
        PositionSmoothingAllowed = true;
    }

    IEnumerator TeleportingTimer()
    {
        photonView.RPC("TeleportingTrue", PhotonTargets.AllBuffered);
        yield return new WaitForSeconds(1);
        photonView.RPC("TeleportingFalse", PhotonTargets.AllBuffered);
    }

    [PunRPC]
    private void TeleportingTrue()
    {
        Teleporting = true;
    }

    [PunRPC]
    private void TeleportingFalse()
    {
        Teleporting = false;
    }
}
This script also lerps between positions and other junk but i dont think that should be the problem. Here is my player controller script as well.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerController : Photon.MonoBehaviour

{
    private PhotonView PhotonView;
    public Text PlayerNameText;

    [SerializeField] private Animator Animator;
    [SerializeField] private SpriteRenderer sr;
    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private GameObject PlayerCamera;
    

    [SerializeField] private float MaxSpeed; // The max speed value of the player's movement
    [SerializeField] private float Acceleration; // The acceleration speed of the player's movement
    [SerializeField] private float Deceleration; // The deceleration speed of the player's movement
    [SerializeField] private float JumpForce; // The jump height value of the player
    [SerializeField] private float Speed; // The speed at which the player is currently moving at
    private int Direction = 0; // Direction the player is pressing on the controller
    public int AnimDirection; // Direction the player is facing for the animator
    private int Flipped; // The direction the character sprite is facing
    private bool isJumping; // If the player is jumping
    public bool isGrounded; // If the player is on the ground
    public bool isUnderRoof; // If the player has blocks above him

    public Transform GroundCheck; // The position of the ground check
    public float CheckRadius; // The area the ground check checks
    public LayerMask WhatIsGround; // What does layer does the ground check check for

    
    void Start()
    {
        PhotonNetwork.sendRate = 3;
        PhotonNetwork.sendRateOnSerialize = 3;
    }

    private void Awake()
    {

        if(photonView.isMine)
        {
            photonView.RPC("Usernames", PhotonTargets.AllBuffered);
            PlayerNameText.text = PhotonNetwork.playerName; // Set player text for local player
        }
    }

    private void Update()
    {
        AnimatorValues();

        if(photonView.isMine)
        {
            CheckInput();
        }
        if(!photonView.isMine)
        {
            photonView.RPC("Usernames", PhotonTargets.AllBuffered);
            PlayerNameText.text = photonView.owner.NickName;
        }

        if(isJumping)
        {
            StartCoroutine(IsJumpingTimer());
        }
    }

    private void FixedUpdate()
    {
        if(photonView.isMine)
        {
            Movement();
            LocalDirectionValues();
        }
        isGrounded = Physics2D.OverlapCircle(GroundCheck.position, CheckRadius, WhatIsGround);
    }
    private void CheckInput()
    {
        if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
        {
            Direction = 1;
        }
        if(Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
        {
            Direction = -1;
        }
        if(Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
        {
            if(isGrounded && transform.localScale.x == -1 && Direction == 1)
            {
                photonView.RPC("FlipRight", PhotonTargets.All); // If on the ground and pressing right, flip player right
            }
        }
        if(Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
        {
            if(isGrounded && transform.localScale.x == 1 && Direction == -1)
            {
                photonView.RPC("FlipLeft", PhotonTargets.AllBuffered); // If on the ground and pressing left, flip player left
            }
        }
        if(isGrounded && Direction == 1 && transform.localScale.x == -1)
        {
            photonView.RPC("FlipRight", PhotonTargets.AllBuffered); // If on the ground and pressing right, flip player right
        }
        if(isGrounded && Direction == -1 && transform.localScale.x == 1)
        {
            photonView.RPC("FlipLeft", PhotonTargets.AllBuffered); // If on the ground and pressing right, flip player right
        }
        if(Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.A))
        {
            Direction = 0;
        }

    }

    private void Movement()
    {
        if(Direction == 1 && Speed < MaxSpeed)
        {
            Speed = Speed + Acceleration; // If pressing right move right
        }
        else if(Direction == -1 && Speed > -MaxSpeed)
        {
            Speed = Speed - Acceleration; // If pressing left move left
        }
        else
        {
            if(Speed > Deceleration)
            {
                Speed = Speed - Deceleration;
            }
            else if(Speed < -Deceleration)
            {
                Speed = Speed + Deceleration;
            }
            else
            {
                Speed = 0;
            }
        }
        if(Speed > MaxSpeed)
        {
            Speed = MaxSpeed;
        }
        if(Speed < -MaxSpeed)
        {
            Speed = -MaxSpeed;
        }

        rb.velocity = new Vector2(Speed, rb.velocity.y);

        if(Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            rb.velocity = Vector2.up * JumpForce; // If pressing jump make player jump using attached rigidbody
            photonView.RPC("IsJumping", PhotonTargets.AllBuffered);
        }
    }

    IEnumerator IsJumpingTimer()
    {
        yield return new WaitForSeconds(0.3f);
        isJumping = false;
    }

    void LocalDirectionValues()
    {
        if(rb.velocity.x > 0)
        {
            AnimDirection = 1;
        }
        else if(rb.velocity.x < 0)
        {
            AnimDirection = -1;
        }
        else if(rb.velocity.x == 0)
        {
            AnimDirection = 0;
        }
    }

    [PunRPC]
    private void IsJumping()
    {
        isJumping = true;       
    }

    [PunRPC]
    private void FlipRight()
    {
        Vector3 scale = transform.localScale;
        scale.x = 1;
        transform.localScale = scale;
        // Flip the character to the right
    }

    [PunRPC]
    private void FlipLeft()
    {
        Vector3 scale = transform.localScale;
        scale.x = -1;
        transform.localScale = scale;
        // Flip the character to the left
    }

    [PunRPC]
    private void Usernames()
    {
        PlayerNameText.color = Color.white; // Set local player text color to white for other players
    }

    void AnimatorValues()
    {
        Animator.SetInteger("Direction", AnimDirection);
        Animator.SetBool("Grounded", isGrounded);
        Animator.SetBool("Jumping", isJumping);
        // Assigned variable values for the animator to use
    }
}

Sorry for all the code but i dont know why so many messages are being sent as i only have a sendrate of 5 messages per second which i assign in the start function of every script. I dont know what is going on and ive been trying to find a solution for weeks with no prevail.

Best Answer

  • JohnTube
    JohnTube ✭✭✭✭✭
    Answer ✓
    Hi @Dyoxide,

    Thank you for choosing Photon!

    You are sending RPCs in Update, every frame! and some are buffered!
    Please review this.

Answers

  • JohnTube
    JohnTube ✭✭✭✭✭
    Answer ✓
    Hi @Dyoxide,

    Thank you for choosing Photon!

    You are sending RPCs in Update, every frame! and some are buffered!
    Please review this.
  • OH MY GOSH IT WORKED THANK YOU SO MUCH. I've been trying to find a solution to this for weeks and i feel kinda dumb that its something this obvious. Not every rpc was being called every frame because i used GetKeyDown on most of them so update would only trigger once and not every frame. It was the usernames rpc that was fricking me up. Thank u for ur help :)
  • How did you measure your messages per second, and what is regarded as an appropriate rate?
  • Dyoxide
    Dyoxide
    edited March 2021
    I checked the messages per second in the photon dash board, and i expected around 20 or below if 2 players are playing (it is around 15 now). I also used the photon stats gui component and checked the amount of messages coming in (receiving) and messages going out (sending). It was really helpful. I thought that RPCs would obey the sendrate, but i guess not because the max amount of messages sent per sec should be 20 if both scripts are set to 5 msg/s sendrate.