Answer the question
In order to leave comments, you need to log in
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);
}
}
}
Answer the question
In order to leave comments, you need to log in
if (!isLocalPlayer)
return;
private void Update()
private void FixedUpdate()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question