3

This code is supposed to generate new food in random positions over the network when the player eats the current one. The code runs smoothly on master client but it's kinda slow on other players.

Any idea what am i doing wrong?

using UnityEngine;
using System.Collections;

public class FoodCollision : Photon.MonoBehaviour
{
    public static float amtOfIncreaseSpeed = 1f;
    public static float amtOfIncreaseHealth = 1f;
    public static float ratioOfSpeed = 150.0f;
    public static float increaseSize = 0.001f;

    PhotonView photonView;
    BoxCollider2D boxCollider;
    SpriteRenderer sp;
    float widthOfBoard, heightOfBoard;
    void Awake()
    {
        photonView = PhotonView.Get(this);
        boxCollider = GetComponent<BoxCollider2D>();
        sp = GetComponent<SpriteRenderer>();
        widthOfBoard = FoodManager.instance.widthOfBoard;
        heightOfBoard = FoodManager.instance.heightOfBoard;
    }

    public void ResetGO()
    {
        sp.enabled = false;
        photonView.RPC("ResetGORPC", PhotonTargets.AllBuffered); 
    }

    [PunRPC]
    void ResetGORPC()
    {
        Vector3 position = new Vector3(Random.Range(-widthOfBoard / 2, widthOfBoard / 2), Random.Range(-heightOfBoard / 2, heightOfBoard / 2), 0);
        transform.position = position;
        transform.localScale = Vector3.one;
        sp.enabled = true;
    }
}
  • Does Profiler show anything interesting? – mgear Jul 01 '16 at 02:18
  • What does "slow" mean? PhotonTargets.AllBuffered is what can affect performance since it accumulates all RPCs triggered from the start of the game and sends all of them to new joined client. This option is not recommended when lot of RPCs used. Try room properties for food positions instead or update new client with these positions from master client when new client joined. – photonians Jul 04 '16 at 13:44

0 Answers0