StationObscurum/Assets/FishNet/Demos/Network LOD/Prefabs/PlayerInputDriver.cs

73 lines
2.0 KiB
C#
Raw Normal View History

2023-05-31 21:50:04 +02:00
using FishNet.Managing;
using FishNet.Managing.Statistic;
2023-05-31 21:12:09 +02:00
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;
2023-05-31 21:50:04 +02:00
[SerializeField] private GameObject spawnable;
2023-05-31 21:12:09 +02:00
private void Start()
{
_characterController = GetComponent(typeof(CharacterController)) as CharacterController;
_jump = false;
2023-05-31 21:50:04 +02:00
2023-05-31 21:12:09 +02:00
}
private void Update()
{
2023-05-31 21:36:32 +02:00
2023-05-31 21:12:09 +02:00
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;
}
}
2023-05-31 21:50:04 +02:00
if (Input.GetKeyDown(KeyCode.Return))
{
GameObject obj = Instantiate(spawnable);
ServerManager.Spawn(obj);
2023-06-01 17:47:55 +02:00
2023-05-31 21:50:04 +02:00
}
2023-05-31 21:12:09 +02:00
_moveDirection.y += gravity * Time.deltaTime;
_characterController.Move(_moveDirection * Time.deltaTime);
}
#region UnityEventCallbacks
public void OnMovement(InputAction.CallbackContext context)
{
2023-05-31 21:36:32 +02:00
2023-05-31 21:12:09 +02:00
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
}