Bad Vertical Movement With Rigidbody2d
Options
I have recently delved in to the world that is networking using PUN. So first and foremost I would like to just say I am a beginner to game networking and any information provided would be extremely appreciated.
For some reason while trying my hand at syncing movement using PUN I'm finding that my vertical movement is extremely jerky. My horizontal movement on the other hand seems to be working relatively well, aside from the fact that bumping into other players causes quite a bit of jitter.
To breakdown what I mean about vertical movement, it seems that when my player jumps there movement seems to be very choppy only on the y direction.
I have posted a video on youtube here demonstrating what I mean
https://youtu.be/-vabAIArkNw
Also here is my code currently for syncing movement.
Lastly both players have a rigidbody2d attached to them and are controlled by this script.
For some reason while trying my hand at syncing movement using PUN I'm finding that my vertical movement is extremely jerky. My horizontal movement on the other hand seems to be working relatively well, aside from the fact that bumping into other players causes quite a bit of jitter.
To breakdown what I mean about vertical movement, it seems that when my player jumps there movement seems to be very choppy only on the y direction.
I have posted a video on youtube here demonstrating what I mean
https://youtu.be/-vabAIArkNw
Also here is my code currently for syncing movement.
using UnityEngine; using System.Collections; public class NetworkPlayer : Photon.MonoBehaviour { public float lerpValue = 0.1f; public float velocityThreshhold; private Vector3 realPosition = Vector3.zero; private Vector2 velocity = Vector2.zero; // Use this for initialization void Start () { } // Update is called once per frame void Update () { Vector3 velocity3D = new Vector3(velocity.x, velocity.y, transform.position.z); if (!photonView.isMine) { transform.position = Vector3.Lerp(transform.position, realPosition, lerpValue) + velocity3D * Time.deltaTime; } } public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { if (stream.isWriting) { stream.SendNext(transform.position); stream.SendNext(transform.localScale); stream.SendNext(GetComponent<Rigidbody2D>().velocity); } else { realPosition = (Vector3) stream.ReceiveNext(); transform.localScale = (Vector3) stream.ReceiveNext(); velocity = (Vector2) stream.ReceiveNext(); Debug.Log("Position: " + realPosition + "Velocity: " + velocity); } } }
Lastly both players have a rigidbody2d attached to them and are controlled by this script.
using UnityEngine; using System.Collections; public class PlayerController : MonoBehaviour { public KeyCode jump, left, right; public float maxSpeed = 10, jumpForce = 7, jumpHoldForce = 5; public LayerMask groundMask; public Transform groundCheck; public bool usingPhoneControls = false; private const string JUMP_TOUCHPAD = "Jump Touchpad"; private ETCTouchPad touchPad; private Animator anim; private bool facingRight = true, isGrounded = false, pressingJump = false; private float groundRadius = 0.2f; private Rigidbody2D _rigidbody2D; private BoxCollider2D _boxCollider; private AudioSource jumpSound; // Use this for initialization void Start () { anim = GetComponent<Animator>(); _rigidbody2D = GetComponent<Rigidbody2D>(); _boxCollider = GetComponent<BoxCollider2D>(); jumpSound = GetComponent<AudioSource>(); touchPad = GameObject.Find(JUMP_TOUCHPAD).GetComponent<ETCTouchPad>(); //adding listeners to touchpad for jumping touchPad.onTouchStart.AddListener(() => { Jump(); }); //adding listener so that we can use hold to jump higher touchPad.onTouchUp.AddListener(() => { //Debug.Log("Released Jump"); ReleaseJump(); }); } // Update is called once per frame void Update () { //if player is grounded and pressing jump with keyboard controls if (!usingPhoneControls && isGrounded && Input.GetButton("Jump")) { Debug.Log("Jumping"); anim.SetBool("Ground", false); GetComponent<Rigidbody2D>().AddForce(new Vector2(0,jumpForce/6), ForceMode2D.Impulse); } } void OnDrawGizmos() { //draws our collider for grounded Gizmos.color = (isGrounded)? new Color(0,1,0, 0.5f) : new Color(1, 0, 0, 0.5f); Gizmos.DrawSphere(groundCheck.position, groundRadius); } void FixedUpdate() { //holding jump so keep adding force if (pressingJump) { //Debug.Log("Holding Jump"); _rigidbody2D.AddForce(new Vector2(0, jumpHoldForce)); } //make a circle underplayer to check if they are grounded isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, groundMask); //set our animation values so that our animations will tie in with our jumping anim.SetBool("Ground", isGrounded); anim.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y); //get left or right from player float move = (!usingPhoneControls)? Input.GetAxis("Horizontal") : ETCInput.GetAxis("Horizontal"); //move the player capping there maxspeed anim.SetFloat("Speed", Mathf.Abs(move)); GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y); //flips animations when facing left or right if (move > 0 && !facingRight) Flip(); else if (move < 0 && facingRight) Flip(); } //flips animations when the player is facing other direction void Flip() { facingRight = !facingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; } //used for phone controls public void Jump() { //if player is grounded and pressing jump if (isGrounded) { jumpSound.Play(); pressingJump = true; anim.SetBool("Ground", false); _rigidbody2D.AddForce(new Vector2(0,jumpForce), ForceMode2D.Impulse); } } //a function to add to ontouchup event public void ReleaseJump() { pressingJump = false; } public bool FacingRight { get { return facingRight; } } public bool IsGrounded { get { return isGrounded; } } }
0
Comments
-
Do you have simulation switched off for character on 2nd client? It's position should be controlled by OnPhotonSerializeView only.0