Using the playermovement script for a 2d Project

Hi!
I have a similar game, but it’s in 2D, so the method you are using here doesn’t really work for me…

is there a way to change my playermovement script into a similar one for mutliplayer purposes?

here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;

public class PlayerMovement : MonoBehaviour
{


    public float moveSpeed = 5f;
    public Rigidbody2D rb;
    public Animator anim;
    Vector2 movement;
    

    private void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");
        

        anim.SetFloat("Horizontal", movement.x);
        anim.SetFloat("Vertical", movement.y);
        anim.SetFloat("Speed", movement.sqrMagnitude);

        if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1 || Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
        {
            anim.SetFloat("lastMoveX", Input.GetAxisRaw("Horizontal"));
            anim.SetFloat("lastMoveY", Input.GetAxisRaw("Vertical"));
        }

        if(movement.magnitude > 1)
        {
            movement.Normalize();
        }
    }

    private void FixedUpdate()
    {
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    }

}

Would really appreciate it!
Thanks for making this series!

Hi there,
A good way to think about breaking it up, is to put the player input on the client side, and execute the actual movement on the server the side.

So your client side update should include the player input part:

movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");

Then it should call to the server using a [Command]. For instance, we call this method in the course CmdMove. You can call this method from fixed update, like you do in the original script.

Inside of that method you can put the rest of your movement logic to be executed on the server.

Does that help at all? Let me know if you want to discuss further.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms