NewInputSystem with Netcode

Helping video

I’m having this strange issue with the new input system using netcode, works perfectly fine on the keyboard but when using the contorller, creates strange issues, I’ve attached a video so u can better understand the issue…

here is the code as well

using System;
using RunEveryone.Player;
using RunEveryone.Player.DataModels;
using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;

namespace RunEveryone.Netcode
{
    [RequireComponent(typeof(PlayerInput))]
    public class PlayerControllerMultiplayer : NetworkBehaviour
    {
        private PlayerInput _playerInput;
        private Vector2 _moveAxis;
        [SerializeField] PlayerMoveProperties _playerMoveProperties;
        [SerializeField] PlayerCinematicProperties _playerCinematicProperties;
        [SerializeField] float AnimationBlendSmoothSpeed = .25f;
        private float _currentBlendX = 0.0f;
        private Rigidbody _rb;
        private Animator _anim;
        private NetworkAnimator _networkAnim;

        private void Awake()
        {
            _playerInput = GetComponent<PlayerInput>();
            _rb = GetComponent<Rigidbody>();
            _anim = GetComponent<Animator>();
            _networkAnim = GetComponent<NetworkAnimator>();
        }
        private void Start()
        {
            // subscribe
            _playerInput.PlayerMoveStartedEvent += OnPlayerMoveStarted;
            _playerInput.PlayerMovePerformedEvent += OnPlayerMovePerformed;
            _playerInput.PlayerMoveCancelledEvent += OnPlayerMoveCancelled;

            // unparent the camera
            _playerCinematicProperties.PlayerCamera.transform.SetParent(null);

        }

        private void OnPlayerMoveCancelled(Vector2 stickAxis)
        {
            if (IsOwner == false) return;

            _moveAxis = stickAxis;
        }

        private void OnPlayerMovePerformed(Vector2 stickAxis)
        {
            if (IsOwner == false) return;
            _moveAxis = stickAxis;
        }

        private void OnPlayerMoveStarted(Vector2 stickAxis)
        {
            if (IsOwner == false) return;
            _moveAxis = stickAxis;
        }

        private void Update()
        {
            if (IsOwner == false) return;
            MovePlayer(_moveAxis, Time.deltaTime);
            RotatePlayer(_moveAxis);

            Debug.Log($"IsOwner => {IsOwner}");

        }

        private void MovePlayer(Vector3 moveDirection, float deltaTime)
        {
            float inputMagnitude = Mathf.Abs(moveDirection.x);

            // Only move if the input magnitude is above the threshold
            if (inputMagnitude > 0.5f)
            {
                // Scale movement based on a framerate-independent delta time
                float targetSpeed = _playerMoveProperties.MoveSpeed * inputMagnitude;

                // Update velocity directly, scaled by deltaTime for consistency across network updates
                Vector3 targetVelocity = Accessories.ConvertToCameraDirection(moveDirection) * targetSpeed;
                _rb.linearVelocity = Vector3.Lerp(_rb.linearVelocity, targetVelocity, _playerMoveProperties.SmoothTime * deltaTime);
            }
            else
            {
                // Stop the player when input is below the threshold
                _rb.linearVelocity = Vector3.zero;
            }

            // Smoothly adjust animation blend values for walking and jogging based on input magnitude
            float targetBlendX = 0.0f;
            if (inputMagnitude > 0.5f && inputMagnitude <= 0.75f)
                targetBlendX = 0.5f; // Walking animation
            else if (inputMagnitude > 0.75f)
                targetBlendX = 1.0f; // Jogging animation

            // Smoothly interpolate _currentBlendX towards targetBlendX for smooth animation
            _currentBlendX = Mathf.Lerp(_currentBlendX, targetBlendX, AnimationBlendSmoothSpeed * deltaTime);

            // Update the animation parameter with the smoothed blend value
            _anim.SetFloat("blendTreeX", _currentBlendX);

            // sending data to the server so it can replicate on other clients as well
            //SendAnimationDataToServerRpc(_moveAxis);

        }

        private void RotatePlayer(Vector3 rotateDirection)
        {

            // Only rotate when the horizontal input is above a certain threshold
            //if (!HasInputAuthority) return;
            if (Mathf.Abs(rotateDirection.x) > 0.1f)
            {
                // Determine the rotation based on input direction
                float rotationY = rotateDirection.x > 0 ? 0f : 180f;

                // Apply the new rotation smoothly for a more natural transition
                transform.rotation = Quaternion.Euler(0, rotationY, 0);
            }
        }

        #region RPC



        #endregion
    }

}


1 Like

I had the same issue, this happens usually when you’re testing your game mainly on your local machine so it kinda detects both inputs running in the background, when you use relay and start testing on different devices, it works fine. So try giving it a test by testing into some other machine/laptop.

1 Like

ok got your point but why isn’t detecting the keyboard the same way its detecting the PS4 controller?
following your logic, it should detect the keyboard as well…!

Well maybe I can’t justify my logic by giving deep hardware technical answers but seems like keyboard is a native I/O device that is directly connected to the hardware while the controller needs a driver to communicate, but anyways you might wanna try it by testing on a different pc.

1 Like

yeah will definitely have to do that but then there’s no point for Unity to provide the extra editors to test Multiplay onto the same machine if its gonna work that way.

1 Like