StationObscurum/Assets/FishNet/Demos/Network LOD/Prefabs/PlayerInputDriver.cs
2023-06-01 11:47:55 -04:00

73 lines
2.0 KiB
C#

using FishNet.Managing;
using FishNet.Managing.Statistic;
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;
[SerializeField] private GameObject spawnable;
private void Start()
{
_characterController = GetComponent(typeof(CharacterController)) as CharacterController;
_jump = false;
}
private void Update()
{
if (!base.IsOwner)
return;
if (_characterController.isGrounded||true)
{
_moveDirection = new Vector3(_moveInput.x, 0.0f, _moveInput.y);
_moveDirection *= speed;
if (_jump)
{
_moveDirection.y = jumpSpeed;
_jump = false;
}
}
if (Input.GetKeyDown(KeyCode.Return))
{
GameObject obj = Instantiate(spawnable);
ServerManager.Spawn(obj);
}
_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
}