Created
November 10, 2015 11:48
-
-
Save castaneai/e5e3945b5fe387924bb8 to your computer and use it in GitHub Desktop.
Move Player with WASD keys (UniRx)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections; | |
| using UnityEngine; | |
| using UniRx; | |
| using UniRx.Triggers; | |
| public class Player : MonoBehaviour { | |
| void Start() { | |
| Func<KeyCode, IObservable<Unit>> onGetKey = (keyCode) => this.UpdateAsObservable().Where(_ => Input.GetKey(keyCode)); | |
| var forward = onGetKey(KeyCode.W).Select(_ => this.transform.forward); | |
| var back = onGetKey(KeyCode.S).Select(_ => -1 * this.transform.forward); | |
| var left = onGetKey(KeyCode.A).Select(_ => Vector3.Cross(this.transform.forward, this.transform.up)); | |
| var right = onGetKey(KeyCode.D).Select(_ => -1 * Vector3.Cross(this.transform.forward, this.transform.up)); | |
| forward.Merge(back).Subscribe(v => this.transform.position += v * Time.deltaTime); | |
| left.Merge(right).Subscribe(v => this.transform.forward = Vector3.Lerp(this.transform.forward, v, Time.deltaTime)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment