A
A
Arseniy Sokolovsky2021-05-30 20:00:09
C++ / C#
Arseniy Sokolovsky, 2021-05-30 20:00:09

Why does the controls work strangely in a game with multiplayer?

I made a 2d game according to the tutorial. The game is controlled using a joystick. I added a Network to the project, created a NetworkManager in the hierarchy, added different components wherever needed. As a result, I can connect to the game, but the control began to work strangely: Video

Here is the player's script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerController : NetworkBehaviour
{
    public float speed;
    public float jumpForce;
    private float moveInput;

    public Joystick joystick;

    private Rigidbody2D rb;

    private bool facingRight = true;

    private bool isGrounded;
    public Transform feetPos;
    public float checkRadius;
    public LayerMask whatIsGround;

    private Animator anim;

    private void Start()
    {
        anim = GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
        joystick = GameObject.Find("Floating Joystick").GetComponent<FloatingJoystick>();
    }

    private void FixedUpdate()
    {
        if (!isLocalPlayer)
            return;
        moveInput = joystick.Horizontal;
        rb.velocity = new Vector2(moveInput * speed,rb.velocity.y);
        if(facingRight== false && moveInput > 0)
        {
            Flip();
        }
        if(facingRight== true && moveInput < 0)
        {
            Flip();
        }
        if(moveInput == 0)
        {
            anim.SetBool("isRunning", false);
        }
        else
        {
            anim.SetBool("isRunning", true);
        }
    }
    private void Update()
    {
        float verticalMove = joystick.Vertical;
        isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
        if(isGrounded == true && verticalMove >= .5f)
        {
            rb.velocity = Vector2.up * jumpForce;
            anim.SetTrigger("TakeOff");
        }
        if(isGrounded == true)
        {
            anim.SetBool("isJumping", false);
        }
        else
        {
            anim.SetBool("isJumping", true);
        }
    }

    void Flip()
    {
        facingRight = !facingRight;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;
        if(moveInput < 0)
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
        }
        else if( moveInput > 0)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
        }
    }
}

I just don't understand what could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DrinkFromTheCup, 2021-06-01
@SokolarGm

if (!isLocalPlayer)
            return;

private void Update()
definitely not needed, but definitely
private void FixedUpdate()
needed?
In addition, even if all checks for whether we take incoming commands from the local player or from the network will succeed - all the commands in a row will try to send (and execute) to all the "dolls" present (and be executed), which (in part - there is no check for horizontal movement) and happened in the last third of the video.
There is no indication of which particular instance of the "doll" the received input will move.
Or something prevented its execution - NetworkBehaviour, as far as I know, by itself does not set the correspondence "object instance" <-> "local / non-local player", which means that somewhere else there must be code that does this.
I can't see any more anomalies with the naked eye...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question