2D Multiplayer Character Flip
So, i am trying to make a 2D side Scroll multiplayer game, but i am having some trouble with the character fliping... I am using the Unity's default 2D Character Controller, and i am fliping with the character Scale. Anyone knows how i make it work? Here is the code so far: private bool m_FacingRight = true;
private void Awake()
{
m_GroundCheck = transform.Find("GroundCheck");
m_CeilingCheck = transform.Find("CeilingCheck");
m_Rigidbody2D = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
m_Grounded = false;
Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
m_Grounded = true;
}
m_Anim.SetBool("Ground", m_Grounded);
m_Anim.SetFloat("vSpeed", m_Rigidbody2D.velocity.y);
}
public void Move(float move, bool crouch, bool jump)
{
if (!crouch && m_Anim.GetBool("Crouch"))
{
if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
{
crouch = true;
}
}
m_Anim.SetBool("Crouch", crouch);
if (m_Grounded || m_AirControl)
{
move = (crouch ? move*m_CrouchSpeed : move);
m_Anim.SetFloat("Speed", Mathf.Abs(move));
m_Rigidbody2D.velocity = new Vector2(move*m_MaxSpeed, m_Rigidbody2D.velocity.y);
if (move > 0 && !m_FacingRight)
{
if (!isLocalPlayer)
RpcFlip();
else
CmdFlip();
}
else if (move < 0 && m_FacingRight)
{
if (!isLocalPlayer)
RpcFlip();
else
CmdFlip();
}
}
if (m_Grounded && jump && m_Anim.GetBool("Ground"))
{
m_Grounded = false;
m_Anim.SetBool("Ground", false);
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
}
}
[Command]
private void CmdFlip()
{
m_FacingRight = !m_FacingRight;
Vector3 flipScale = BodyToFlip.localScale;
flipScale.x *= -1;
BodyToFlip.localScale = flipScale;
}
[ClientRpc]
private void RpcFlip()
{
m_FacingRight = !m_FacingRight;
Vector3 flipScale = BodyToFlip.localScale;
flipScale.x *= -1;
BodyToFlip.localScale = flipScale;
}
}
Comment
What exactly are you having problems with, are you getting any errors in the console window? Also where is BodyToFlip declared? You are currently assigning a position to a variable that either doesn't exist or isn't instantiated from the look of things.
@Cepheid The BodyToFlip is a transform added via inspector. The problem is that i cant sync the scale on a multiplayer game.