unity-atoms/Examples/Assets/UniRx/Player/PlayerMoveUniRx.cs

44 lines
1.3 KiB
C#
Raw Normal View History

2020-03-20 01:29:39 +01:00
using UnityEngine;
2020-03-02 02:26:06 +01:00
using Marvelous;
using UniRx;
using UnityAtoms.BaseAtoms;
2019-10-09 00:13:33 +02:00
2020-03-02 02:26:06 +01:00
namespace UnityAtoms.Examples
{
2020-03-20 01:29:39 +01:00
/// <summary>
/// Simple Player move script using UniRx.
/// </summary>
2020-03-02 02:26:06 +01:00
public class PlayerMoveUniRx : MonoBehaviour
{
[SerializeField]
private StringVariable _uiState;
[SerializeField]
private StringConstant _uiStatePlaying;
2019-10-09 00:13:33 +02:00
2020-03-02 02:26:06 +01:00
private void Awake()
{
float _horizontal = 0f, _vertical = 0f;
string HORIZONTAL = "Horizontal", VERTICAL = "Vertical";
2019-10-09 00:13:33 +02:00
2020-03-20 01:29:39 +01:00
Observable
.EveryUpdate()
.Fuse<long, string>(
_uiState.ObserveChange(),
initialValue2: _uiState.Value
).Subscribe(t =>
{
var (_, state) = t;
_horizontal = state == _uiStatePlaying.Value ? Input.GetAxis(HORIZONTAL) : 0f;
_vertical = state == _uiStatePlaying.Value ? Input.GetAxis(VERTICAL) : 0f;
});
2019-10-09 00:13:33 +02:00
2020-03-20 01:29:39 +01:00
Observable
.EveryFixedUpdate()
.Subscribe(t =>
{
GetComponent<Rigidbody2D>().velocity = new Vector2(_horizontal, _vertical) * 5f;
});
2020-03-02 02:26:06 +01:00
}
}
}