🐠
Fishnet Quickstart
  • Introduction
  • Installation
  • Basic Player Movement
  • Advanced Player Movement
Powered by GitBook
On this page
  • Basic Scene Setup
  • Player Movement

Basic Player Movement

Assumption: You have installed all the dependencies from the previous step

Basic Scene Setup

Create the Manager Objects in your scene

  • (optional) Create a new folder called _Game in your assets folder so your assets are kept separate from imported assets

  • (optional) Move your default Scenes folder into that new _Game folder

  • Add a plane to the scene at orgin, Adjust the Camera and Lighting to your liking, add whatever else you want to the scene. Then save.

  • Create an empty game object at the root of your scene called NetworkManager and Add following FishNet scripts:

    1. NetworkManager

    2. Player Spawner

    3. (optional) Tugboat ( fishnet will add this anyway as the default transport, just here so you know there are different transports that you can choose from.)

  • Set the 'Spawnable Prefabs' to 'DefaultPrefabObjects'

  • Find and place the /FishNet/Example/Prefabs/NetworkHudCanvas as a child of the NetworkManager you just created.

  • (optional) Add the Fishnet 'PingDisplay' script as a component on the network manager

  • Save Scene

Create the Player Prefab

  • Add an empty game object called 'Player' and add the following FishNet scripts:

    1. Network Object

    2. Network Trasform

  • add Capsule to the Player object as a Child to graphically represent it

  • Position the Player object slightly above the plane.

  • Create a prefab out of it:

    • create a prefab folder under _Game

    • drag the object you just created into

    • then delete the object in your scene.

  • Drag this player prefab into the player spawner component on your Basic scene network manager and save the scene

Test networking and spawn ( not movement yet )

  • launch the game in your editor and make sure that once you click on 'start server', then 'start client' the player spawns.

  • The player does not yet have movement added, we are just testing that it spawns and networking is working

If everything seems to be working and your project looks right move on to the next section.

Player Movement

Requires that you have installed Unity's new Input System package from the list of dependencies. It's a tiny bit more to set up, but it's superior and you'll want to use it eventually anyway.

Add the input bits to the player object and configure the input system.

  • Create a new script on the player prefab called 'PlayerInputDriver' (or whatever you would like I suppose) and save it in a new _Game/Scripts folder.

  • Edit the player input driver script you created to look like this

using FishNet.Object;
using UnityEngine;
using UnityEngine.InputSystem;

[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(PlayerInput))]
public class PlayerInputDriver : NetworkBehaviour
{
    private CharacterController _characterController;
    private Vector2 _moveInput;
    private Vector3 _moveDirection;
    private bool _jump;
    [SerializeField] public float jumpSpeed = 6f;
    [SerializeField] public float speed = 8f;
    [SerializeField] public float gravity = -9.8f;
    private void Start()
    {
        _characterController = GetComponent(typeof(CharacterController)) as CharacterController;
        _jump = false;
    }
    private void Update()
    {
        if (!base.IsOwner)
            return;
        if (_characterController.isGrounded)
        {
            _moveDirection = new Vector3(_moveInput.x, 0.0f, _moveInput.y);
            _moveDirection *= speed;

            if (_jump)
            {
                _moveDirection.y = jumpSpeed;
                _jump = false;
            }
        }
        _moveDirection.y += gravity * Time.deltaTime;
        _characterController.Move(_moveDirection * Time.deltaTime);
    }
    #region UnityEventCallbacks
    public void OnMovement(InputAction.CallbackContext context)
    {
        if (!base.IsOwner)
            return;
        _moveInput = context.ReadValue<Vector2>();
    }
    public void OnJump(InputAction.CallbackContext context)
    {
        if (!base.IsOwner)
            return;
        if (context.started || context.performed)
        {
            _jump = true;
        }
        else if (context.canceled)
        {
            _jump = false;
        }
    }
    #endregion
}

The above window is a properly configured input action assset from the Input System package. Follow the directions below to add the PlayerInput object to the prefab and configure unity events from the input package to hit your script methods.

    • During Step 2 of that process add an addition jump action as shown. .

      • Hint: for the binding use the 'listen' button where it says path and press space, then select what is shown.

      • Remember to press 'Save Asset' before you exit the input acton editor

  • Switch the 'Behavior' of the Player Input component to 'Invoke Unity Events'.

  • Wire up the unity events from the input system to the script you created as shown

  • Close the prefab to save it

Test

Fire up multiple instances using ParalSync or a build + editor. Have one of them be the server and a client, the others just be clients. Everything should work as expected for player movement.

Have a cold beer.

PreviousInstallationNextAdvanced Player Movement

Last updated 2 years ago

You can test multiple versions of your game in multiple editors without having to build using . I will not cover that here but it's the easiest way to dev multiplayer games that I know of. You can also build the player and test that way.

adding the PlayerInput object to the player prefab you created earlier.

Parelsync
Follow the Quickstart