Search Unity

some lags and gitters when moving character need help

Discussion in 'Multiplayer' started by mambo_2, Jul 1, 2014.

  1. mambo_2

    mambo_2

    Joined:
    Aug 19, 2013
    Posts:
    19
    Hello , i'm working on a 2d fighting/platformer game and my problem is when I move my character it's fine on my side but on the other side it's a bit laggy and gitterish specially with the jump , i am connecting directly to the ip not master server is being used so from my understanding internet speed has nothing to do with it , here's my code if anyone could point out what i'm doing wrong i'll be thankful

    using UnityEngine;
    using System.Collections;

    public class RobotControllerScript : MonoBehaviour {


    public float maxSpeed = 10f;
    public bool FacingRight = true;
    public bool isGrounded = false;

    public Transform groundCheck;

    float groundRadius = 0.2f;

    public LayerMask whatIsGround;

    public float jumpVelocity = 10f;
    public bool doublejump = false;
    public float Move;
    Animator anim;
    Vector3 bulletPos;
    public bool nashmiModeActivated = false;
    bool moveAnim = false;
    public float characterVelocityX;
    public float characterVelocityY;
    public float characterPositionX;
    public float characterPositionY;
    public float characterScaleX;

    public static int startRpcDelay = 0;
    public static int delayRpc = 0;

    public static bool startRPC = false;
    public bool jump = false;
    float allowedJumpFrames = 5;
    float jumpFrames;

    // Use this for initialization
    void Start ()
    {

    anim = GetComponent<Animator> ();





    }

    [RPC]
    void runAnimation(string Run, bool isRunning){
    anim.SetBool (Run, isRunning);
    }



    [RPC]
    void JumpAnimation(string Jump, bool isJumping , string Fall , float fallingSpeed){
    anim.SetBool (Jump, isJumping);
    anim.SetFloat (Fall, fallingSpeed);
    }



    // Update is called once per frame
    void FixedUpdate ()
    {


    isGrounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);



    if (isGrounded)
    doublejump = false;

    if ((Move > 0 && !FacingRight))
    flip();
    else
    if ((Move < 0 && FacingRight))
    flip ();


    }

    void Update()
    {


    if (Mathf.Abs (Move) > 0) {
    moveAnim = true;

    } else {

    moveAnim = false;
    }



    if (networkView.isMine) {
    getInput ();
    moveCharacter (Move, jump);


    if (startRPC) {
    delayRpc++;
    if (delayRpc > 150) {
    networkView.RPC ("runAnimation", RPCMode.All, "Speed", moveAnim);
    networkView.RPC ("JumpAnimation", RPCMode.All, "Ground", isGrounded, "vSpeed", rigidbody2D.velocity.y);

    networkView.RPC ("moveCharacter", RPCMode.All, Move, jump);
    networkView.RPC("checkTransform" , RPCMode.Others , this.transform.localScale , this.transform.position);

    }
    }

    }
    }









    void flip()
    {
    FacingRight = !FacingRight;
    Vector3 theScale = this.transform.localScale;

    theScale.x *= -1;
    this.transform.localScale = theScale;




    }

    void getInput()
    {
    if (Input.GetKey (KeyCode.JoystickButton4)) {
    jumpFrames +=0.2f;
    if(jumpFrames > allowedJumpFrames)
    {

    jumpFrames = allowedJumpFrames;
    }

    }

    if (Input.GetKeyUp (KeyCode.JoystickButton4)) {
    jump = true;
    } else {
    jump = false;
    }

    Move = Input.GetAxis ("Horizontal");





    }

    [RPC]
    void checkTransform( Vector3 characterScale , Vector3 characterPosition)
    {
    this.transform.position = characterPosition;
    this.transform.localScale = characterScale;
    }




    [RPC]
    void moveCharacter(float movePlayer , bool activateJump )
    {

    if (nashmiModeActivated) {

    maxSpeed = 15f;
    } else {
    maxSpeed = 10f;
    }


    if (( isGrounded || !doublejump) && (activateJump))
    {

    isGrounded = false;
    if(!isGrounded && !doublejump)
    doublejump = true;

    Vector2 vel = rigidbody2D.velocity;
    vel.y = jumpVelocity*jumpFrames;
    rigidbody2D.velocity = vel;


    jumpFrames = 0;


    }



    rigidbody2D.velocity = new Vector2 (movePlayer * maxSpeed, rigidbody2D.velocity.y);

    }











    }
     
  2. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    https://developer.valvesoftware.com/wiki/Source_Multiplayer_Networking

    Will give you an better idea of handling a multiplayer game.

    1. You shouldn't send RPC's each frame.
    2. You both set position and tells it to move using physical movement.
    3. You cannot count on PhysX to work the same way on all clients. So dont let people calculate their own physics simulation.
    4. You should not send your RPCS to all. You move your own character AND send new information to it too. Send to others only.

    And more.

    Again the paper will maybe give you an idea of how to set it up.
     
    Last edited: Jul 1, 2014
  3. mambo_2

    mambo_2

    Joined:
    Aug 19, 2013
    Posts:
    19
    @BFGames Thank you a lot for your answer I know it's late but really helped me when I needed to understand more about networking :).
     
  4. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    No problem man.