Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question new input system and multyplayer together making some mess for me

Discussion in 'Netcode for GameObjects' started by firebird721, Aug 1, 2023.

  1. firebird721

    firebird721

    Joined:
    Jun 8, 2022
    Posts:
    101
    hi - have controler of player that supoose to set state of fire button and set it to network variable in other scripts that handle shooting by server

    i dont know why i cant seem to priduce simple effect thru inptsystem


    using UnityEngine;
    using UnityEngine.InputSystem;
    using Unity.Netcode;

    public enum ButtonFireState
    {
    ButtonPress,
    ButtonRelease,
    ButtonHold,
    ButtonNone
    }
    public class NI_CharacterControllerMain : NetworkBehaviour
    {
    public NetworkVariable<ButtonFireState> fireButtonState = new NetworkVariable<ButtonFireState>();
    [Range(1, 50)]
    public float speed = 4f;
    private const float INPUT_DEADZONE = 0.1f;
    private Rigidbody2D rb;
    private NetworkVariable<Vector2> movementInput = new NetworkVariable<Vector2>();
    private Vector2 currentVelocity;
    [SerializeField]
    private InputActionReference fireAction;
    [SerializeField]
    private InputActionReference movementAction;
    private const float FIRE_BUTTON_HOLD_TIME = 0.2f;
    private const float MOVEMENT_SMOOTH_TIME = 0.15f;
    private float buttonPressTime;
    void Awake()
    {
    rb = GetComponent<Rigidbody2D>();
    }
    public override void OnNetworkSpawn()
    {
    if (IsClient && IsOwner)
    {
    fireAction.action.Enable();
    fireAction.action.started += ctx => HandleFireActionStart();
    fireAction.action.canceled += ctx => HandleFireActionEnd();
    movementAction.action.Enable();
    movementAction.action.performed += ctx => HandleMovementAction(ctx.ReadValue<Vector2>());
    movementAction.action.canceled += ctx => HandleMovementAction(Vector2.zero);
    }
    }
    [ServerRpc]
    void MovementServerRpc(Vector2 movement)
    {
    movementInput.Value = movement.sqrMagnitude > INPUT_DEADZONE ? movement.normalized : Vector2.zero;
    }
    void FixedUpdate()
    {
    if (IsServer)
    {
    Vector2 targetVelocity = movementInput.Value * speed;
    rb.velocity = movementInput.Value != Vector2.zero
    ? Vector2.SmoothDamp(rb.velocity, targetVelocity, ref currentVelocity, MOVEMENT_SMOOTH_TIME)
    : Vector2.zero;
    }
    }
    void OnDisable()
    {
    fireAction.action.Disable();
    fireAction.action.started -= ctx => HandleFireActionStart();
    fireAction.action.canceled -= ctx => HandleFireActionEnd();
    movementAction.action.Disable();
    movementAction.action.performed -= ctx => HandleMovementAction(ctx.ReadValue<Vector2>());
    movementAction.action.canceled -= ctx => HandleMovementAction(Vector2.zero);
    }
    private void HandleFireActionStart()
    {
    buttonPressTime = Time.time;
    }
    private void HandleFireActionEnd()
    {
    if ((Time.time - buttonPressTime) > FIRE_BUTTON_HOLD_TIME)
    {
    Debug.Log("ButtonHold");
    SetFireButtonServerRpc(ButtonFireState.ButtonHold);
    }
    else
    {
    Debug.Log("ButtonPress");
    SetFireButtonServerRpc(ButtonFireState.ButtonPress);
    }
    // Consider the button release state
    Debug.Log("ButtonRelease");
    SetFireButtonServerRpc(ButtonFireState.ButtonRelease);
    }
    private void HandleMovementAction(Vector2 movement)
    {
    if ((movement - movementInput.Value).sqrMagnitude > 0.01f)
    {
    MovementServerRpc(movement);
    }
    }
    public ButtonFireState GetButtonState()
    {
    if ((Time.time - buttonPressTime) > FIRE_BUTTON_HOLD_TIME)
    {
    Debug.Log("ButtonHold");
    return ButtonFireState.ButtonHold;
    }
    else if (fireAction.action.triggered)
    {
    Debug.Log("ButtonPress");
    return ButtonFireState.ButtonPress;
    }
    Debug.Log("ButtonNone");
    return ButtonFireState.ButtonNone;
    }
    [ServerRpc]
    private void SetFireButtonServerRpc(ButtonFireState state)
    {
    fireButtonState.Value = state;
    }
    }