Cz_mirror Game開発備忘録

週末にUnity でゲーム開発をしています。ゲーム開発を通じて得た情報の備忘録として活用するブログになります。

【RAYSER開発】プレイヤーの動きをUniRxで実装してみました。

RAYSERの自機の動きはキー操作で制御していますが、こちらの制御でUniRxを用いております。

(キー操作が行われた時に、Move関数を実行する程度の簡単なものになります。)

 

unityroom.com

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniRx;
using UniRx.Triggers;

public class PlayerMove : MonoBehaviour
{
public float moveSpeed; // 現在のスピード

private void Start()
{
// UniRX 移動処理を実施
this.UpdateAsObservable()
.Where(_ =>
(Input.GetAxis("Horizontal") != 0) ||
(Input.GetAxis("Vertical") != 0)
)
.Subscribe(_ => Move());
}

void Move () {

var x = Input.GetAxis("Horizontal") * moveSpeed;
var z = Input.GetAxis("Vertical") * moveSpeed;

if (x != 0 || z != 0)
{
var direction = new Vector3(x, 0, z);
transform.position += new Vector3(x * Time.deltaTime,0, z * Time.deltaTime);
transform.localRotation = Quaternion.LookRotation(direction);
}

}

}