2021-01-04 17:49:48 +01:00
|
|
|
using Ktyl.Util;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
using PlayerInput = Input.PlayerInput;
|
|
|
|
|
|
|
|
public class MovePlayer : MonoBehaviour
|
|
|
|
{
|
|
|
|
[SerializeField] private SerialFloat speed;
|
|
|
|
[SerializeField] private RectOffset extents;
|
2021-01-09 17:18:29 +01:00
|
|
|
[SerializeField] private float lerpAmount = 0.95f;
|
2021-01-09 17:52:16 +01:00
|
|
|
[SerializeField] private SerialFloat horizontalInput;
|
2021-01-10 06:55:45 +01:00
|
|
|
[SerializeField] private SerialFloat horizontalPosition;
|
2021-01-11 03:13:03 +01:00
|
|
|
[SerializeField] private SerialFloat zPosition;
|
2021-01-04 17:49:48 +01:00
|
|
|
|
|
|
|
private PlayerInput _input;
|
|
|
|
|
|
|
|
private Vector2 _currentInput;
|
|
|
|
|
|
|
|
private Transform _transform;
|
2021-01-09 17:18:29 +01:00
|
|
|
|
2021-01-04 17:49:48 +01:00
|
|
|
private Rect _pos;
|
|
|
|
|
2021-01-09 17:18:29 +01:00
|
|
|
private float _yPos;
|
|
|
|
private float _zPos;
|
|
|
|
|
2021-01-04 17:49:48 +01:00
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
_transform = transform;
|
2021-01-09 17:18:29 +01:00
|
|
|
_yPos = _transform.localPosition.y;
|
|
|
|
_zPos = _transform.localPosition.z;
|
2021-01-04 17:49:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
{
|
|
|
|
_input ??= new PlayerInput();
|
|
|
|
|
|
|
|
_input.Enable();
|
|
|
|
_input.Default.Move.performed += DoMove;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
{
|
|
|
|
_input.Disable();
|
|
|
|
_input.Default.Move.performed -= DoMove;
|
|
|
|
}
|
|
|
|
|
2021-01-10 06:55:45 +01:00
|
|
|
private float _x;
|
|
|
|
|
2021-01-04 17:49:48 +01:00
|
|
|
private void Update()
|
|
|
|
{
|
|
|
|
_transform.localPosition += (Vector3) _currentInput * speed * Time.deltaTime;
|
2021-01-10 06:55:45 +01:00
|
|
|
|
|
|
|
var w = Mathf.Abs(extents.right - extents.left);
|
|
|
|
var x = _transform.localPosition.x;
|
|
|
|
if (x > extents.right)
|
|
|
|
{
|
|
|
|
x -= w;
|
|
|
|
}
|
|
|
|
else if (x < extents.left)
|
|
|
|
{
|
|
|
|
x += w;
|
|
|
|
}
|
2021-01-09 17:18:29 +01:00
|
|
|
|
2021-01-04 17:49:48 +01:00
|
|
|
_transform.localPosition = new Vector3(
|
2021-01-10 06:55:45 +01:00
|
|
|
x,
|
2021-01-09 17:18:29 +01:00
|
|
|
_yPos,
|
|
|
|
_zPos);
|
2021-01-09 17:52:16 +01:00
|
|
|
|
|
|
|
horizontalInput.Value = _currentInput.x;
|
2021-01-10 06:55:45 +01:00
|
|
|
horizontalPosition.Value = _transform.localPosition.x;
|
2021-01-11 03:13:03 +01:00
|
|
|
zPosition.Value = transform.position.z;
|
2021-01-04 17:49:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void DoMove(InputAction.CallbackContext context)
|
|
|
|
{
|
|
|
|
var value = context.ReadValue<Vector2>();
|
2021-01-09 17:18:29 +01:00
|
|
|
_currentInput = Vector2.Lerp(_currentInput, value, lerpAmount);
|
2021-01-04 17:49:48 +01:00
|
|
|
}
|
|
|
|
}
|