Search Unity

Other Players Sprites dont Flip

Discussion in 'Multiplayer' started by _diov, Apr 27, 2022.

  1. _diov

    _diov

    Joined:
    Apr 5, 2022
    Posts:
    1
    Hello, im making a 2d Game with Mirror, i bult an dedicated server for Linux and can connect Players on it, Everyone sees Everyone movement and animation but doesnt change the facing direction for other players (those who dont are isLocalPlayer)

    This is my Player code:


    Code (CSharp):
    1. using Mirror;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Player : NetworkBehaviour
    7. {
    8.     public float speed = 10f;
    9.     public float jumpForce = 100f;
    10.  
    11.     public Transform groundCheck;
    12.     public LayerMask groundLayer;
    13.     public float groundCheckRadius;
    14.  
    15.     private Vector2 _movement;
    16.  
    17.     private Rigidbody2D _rigidBody;
    18.     private Animator _animator;
    19.  
    20.     [SyncVar]
    21.     public bool _facingRight = true;
    22.     private bool _isGrounded;
    23.     private bool doubleJump = true;
    24.     private bool isAttacking = false;
    25.     [SyncVar]
    26.     public bool isAlive = true;
    27.  
    28.     private void Awake()
    29.     {
    30.         _rigidBody = GetComponent<Rigidbody2D>();
    31.         _animator = GetComponent<Animator>();
    32.     }
    33.     // Start is called before the first frame update
    34.     public override void OnStartLocalPlayer()
    35.     {
    36.         Camera.main.GetComponent<CameraFollow>().setTarget(transform);
    37.     }
    38.  
    39.     void Update()
    40.     {
    41.         if (!hasAuthority || !isAlive)
    42.         {
    43.             return;
    44.         }
    45.         if (isLocalPlayer)
    46.         {
    47.             float horizontalInput = Input.GetAxisRaw("Horizontal");
    48.             if (horizontalInput < 0f && _facingRight == true)
    49.             {
    50.                 Flip();
    51.             }
    52.             else if (horizontalInput > 0f && _facingRight == false)
    53.             {
    54.                 Flip();
    55.             }
    56.            
    57.  
    58.             _isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
    59.  
    60.             if (Input.GetButtonDown("Jump") && (_isGrounded == true || doubleJump == true))
    61.             {
    62.                 doubleJump = false;
    63.                 _rigidBody.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
    64.             }
    65.  
    66.            
    67.  
    68.             _movement = new Vector2(horizontalInput, 0f);
    69.             if (_movement != Vector2.zero)
    70.             {
    71.                 _animator.SetInteger("animation", 2);
    72.             }
    73.  
    74.             if (Input.GetButtonDown("Fire1") && isAttacking == false)
    75.             {
    76.                 _animator.SetInteger("animation", 1);
    77.             }
    78.  
    79.         }
    80.     }
    81.  
    82.     private void FixedUpdate()
    83.     {
    84.         float horizontalVelocity = _movement.normalized.x * speed;
    85.         _rigidBody.velocity = new Vector2(horizontalVelocity, _rigidBody.velocity.y);
    86.     }
    87.  
    88.     private void LateUpdate()
    89.     {
    90.         if (!isAlive)
    91.         {
    92.             return;
    93.         }
    94.         if (_movement == Vector2.zero)
    95.         {
    96.             _animator.SetInteger("animation", 0);
    97.         }
    98.         if (_isGrounded == true && doubleJump == false)
    99.         {
    100.             doubleJump = true;
    101.         }
    102.     }
    103.  
    104.     private void Flip()
    105.     {
    106.         if(!isLocalPlayer) return;
    107.         _facingRight = !_facingRight;
    108.         float localScaleX = transform.localScale.x;
    109.         localScaleX = localScaleX * -1f;
    110.         transform.localScale = new Vector3(localScaleX, transform.localScale.y, transform.localScale.z);
    111.     }
    112.  
    113.     public void Die()
    114.     {
    115.       isAlive = false;
    116.       _animator.SetInteger("animation", 5);
    117.     }
    118.  
    119. }
    120.  
    Thanks
     
  2. toddkc

    toddkc

    Joined:
    Nov 20, 2016
    Posts:
    207
    Nothing in your code will make networked players flip. That's the problem.