public Transform t;
public Rigidbody2D rb;
public PhotonView view;
public Sprite[] slashSprites;
public float slashDuration;
public float speed;
public float slashDistance;
Joystick joystickMove;
Joystick joystickKick;
Vector2 movement;
Vector2 kickDirection;
bool kickTheBall=false;
public float kickForce;
public LayerMask ballLayerMask;
private void Start()
{
if (view.IsMine) {
joystickMove = GameObject.Find("JoystickMove").GetComponent<Joystick>();
joystickKick = GameObject.Find("JoystickKick").GetComponent<Joystick>();
}
}
void Update()
{
if (view.IsMine)
{
if (joystickMove.Horizontal >= 0.01 || joystickMove.Horizontal <= -0.01)
{
movement.x = joystickMove.Horizontal;
}
else
movement.x = 0;
if (joystickMove.Vertical >= 0.01 || joystickMove.Vertical <= -0.01)
{
movement.y = joystickMove.Vertical;
}
else
movement.y = 0;
if (joystickKick.Horizontal == 0 && joystickKick.Vertical == 0 && kickTheBall)
{
kickTheBall = false;
photonView.RPC("KickBall", RpcTarget.All,kickDirection.normalized,kickForce);
}
else if (Mathf.Abs(joystickKick.Horizontal) >= 0.1f || Mathf.Abs(joystickKick.Vertical) >= 0.1f)
{
kickTheBall = true;
kickDirection = new Vector2(joystickKick.Horizontal, joystickKick.Vertical);
}
}
}
private void FixedUpdate()
{
if (view.IsMine)
{
rb.MovePosition((Vector2)t.position + movement.normalized* speed * Time.fixedDeltaTime);
}
#PUN 2 beginner question
1 messages · Page 1 of 1 (latest)
This was part one here comes the other
[PunRPC]
public void KickBall(Vector2 kickDirection,float force)
{
Collider2D coll = Physics2D.OverlapCircle(t.position, 1.25f,ballLayerMask);
print(Vector2.SignedAngle(Vector2.right, kickDirection));
if(coll !=null)
{
Debug.DrawLine(coll.transform.position, (Vector2)coll.transform.position + kickDirection,Color.red,3f);
Rigidbody2D rbBall=coll.gameObject.GetComponent<Rigidbody2D>();
rbBall.AddForce(kickDirection* force, ForceMode2D.Impulse);
}
}
UNANSWERD: PUN 2 beginner question
Okay it seems likes the part of the code that is tracking joystickKick and calling the Kick function is causing lag issues
I tried putting it into comments And lag disappears, what should I do now?
any solutions or code rewrites?
Sounds like you may be sending a lot of RPCs from Update, try logging your RPC and if-statement to ensure its only being fired once, also its best to try and minimize how much traffic you send over the network when possible, so if you can do the RPC locally, you may not need to send RPCTarget All, and since kickForce is a variable on the script, I imagine both have the same value for this already unless your modifying it beforree sending (which doesnt look like you are in the code you shown), you may not need to send that extra param to your RPC, as every param is extra data per client
PUN 2 beginner question
Could you elaborate on logging the Rock and if statement? And did you mean that I do not need to call the rpc for kickBall because it's movement will already get tracked by photon view transform, with "you don't need rpcyTarget all" ( what other target would I use?). I am pretty new to photon so I don't really understand some of those terms and server client relationships ...
Thats fair, Photon, and multiplayer in general can be complicated
Could you elaborate on logging the Rock and if statement?
Sure, what I meant was adding aDebug.Loginside the first line of your RPC function so you can see how often it actually is being called, and also another log of all the values of your if-statement that calls your RPCs, so you can see how many frames your if-statement is true for - if the RPC is called more than once or your if-statement is firing for more than 1 frame, or both, you know there could potentially be a problem there and you may be sending a lot of events per frame or per second, both would be a high volume which can cause issues
did you mean that I do not need to call the rpc for kickBall because it's movement will already get tracked by photon view transform ( what other target would I use?)
In general, if you can avoid calling a RPC in Update (or very frequently) that is often ideal, and if you are able to perform logic locally for the sender, and re-simulate the logic locally for receivers with minimal data, that is also often ideal, what I was suggesting is that you could useRPCTarget.Others, instead of.All- say there are 3 players in the room, using.Allmeans all 3 players (including the sender) get a event and have to wait for a request over the network, but in most cases the sender doesnt need to do this, since they can just call the function without an RPC after sending the RPC to the other 2 players, this way only 2 events are sent that frame, its not much but per-frame, or per-second, it can add up quickly