Skip to content

Commit 3b24f9c

Browse files
committed
Working Reactive Movement
1 parent 43fd274 commit 3b24f9c

File tree

14 files changed

+218
-167
lines changed

14 files changed

+218
-167
lines changed

Assets/CameraController.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
[ExecuteInEditMode]
6+
public class CameraController : MonoBehaviour
7+
{
8+
9+
public GameObject Player;
10+
private Vector3 offset;
11+
12+
void Start()
13+
{
14+
offset = transform.position - Player.transform.position;
15+
}
16+
17+
// Update is called once per frame
18+
void LateUpdate()
19+
{
20+
transform.position = Player.transform.position + offset;
21+
22+
}
23+
}

Assets/Scripts/AI/ReactiveTEST/ReactiveBehaviorTreeElement.cs.meta renamed to Assets/CameraController.cs.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/InputHandler.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UniRx;
5+
using UniRx.Triggers;
6+
using UnityEngine;
7+
8+
public class InputHandler : MonoBehaviour
9+
{
10+
public IObservable<Vector2> Movement { get; private set; }
11+
12+
private void Awake()
13+
{
14+
Movement = this.FixedUpdateAsObservable()
15+
.Select(_ => {
16+
var x = Input.GetAxis("Horizontal");
17+
var y = Input.GetAxis("Vertical");
18+
return new Vector2(x, y).normalized;
19+
});
20+
}
21+
}

Assets/InputHandler.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/PlayerController.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UniRx;
4+
using UnityEngine;
5+
6+
public class PlayerController : MonoBehaviour
7+
{
8+
public InputHandler inputs;
9+
10+
public float walkSpeed = 0.5f;
11+
private CharacterController character;
12+
// Use this for initialization
13+
private void Start()
14+
{
15+
character = FindObjectOfType<CharacterController>();
16+
inputs.Movement
17+
.Where(v => v != Vector2.zero)
18+
.Subscribe(inputMovement => {
19+
var inputVelocity = inputMovement * walkSpeed;
20+
21+
var playerVelocity =
22+
inputVelocity.x * transform.right +
23+
inputVelocity.y * transform.forward;
24+
25+
var distance = playerVelocity * Time.fixedDeltaTime;
26+
character.Move(distance);
27+
}).AddTo(this);
28+
}
29+
}

Assets/PlayerController.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/AI/Components/Selector.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,24 @@ namespace Assets.Scripts.AI.Components
1212
/// Returns and is in success state if a child was successful, otherwise returns in fail state
1313
/// </summary>
1414
[Serializable]
15-
[Description("Runs children in order")]
15+
[Description("Runs children in order. Succeeds on first child that succeeds.")]
1616
public class Selector : BehaviorComponent
1717
{
1818
public Selector(string name, int depth, int id)
1919
: base(name, depth, id)
2020
{ }
2121

22-
public override IEnumerator Tick(UnityEngine.WaitForSeconds delayStart = null)
22+
public override IEnumerator Tick(WaitForSeconds delayStart = null)
2323
{
2424
base.Tick().ToObservable().Subscribe(xb => Debug.Log("Subscribed to Selector at start (base.tick()"));
2525

2626
CurrentState = (BehaviorState.Running);
2727
foreach (BehaviorTreeElement behavior in Children)
2828
{
29-
yield return BehaviorTreeManager.StartCoroutine(behavior.Tick());
29+
yield return behavior.Tick().ToObservable().Subscribe(_ =>
30+
{
31+
32+
});
3033

3134
if (behavior.CurrentState != BehaviorState.Fail)
3235
{

Assets/Scripts/AI/Components/Sequencer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public Sequencer(string name, int depth, int id)
1414
{
1515
}
1616

17-
public override IEnumerator Tick(UnityEngine.WaitForSeconds delayStart = null)
17+
public override IEnumerator Tick(WaitForSeconds delayStart = null)
1818
{
1919
yield return delayStart;
2020
CurrentState = BehaviorState.Running;

Assets/Scripts/AI/Decorators/Inverter.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public override IEnumerator Tick(WaitForSeconds delayStart = null)
2222

2323
yield return behavior.Tick().ToObservable().Subscribe(_ =>
2424
{
25-
Debug.Log("Inverting " + behavior);
25+
Debug.LogError("Inverting " + behavior);
2626
switch (behavior.CurrentState)
2727
{
2828
case BehaviorState.Fail:
@@ -38,11 +38,7 @@ public override IEnumerator Tick(WaitForSeconds delayStart = null)
3838
Debug.LogError("Something went wrong in an inverter.");
3939
break;
4040
}
41-
}
42-
43-
44-
);
45-
41+
});
4642
}
4743
}
4844
}

Assets/Scripts/AI/Nodes/BehaviorNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public BehaviorNode(string name, int depth, int id) : base(name, depth, id)
1212

1313
public override IEnumerator Tick(WaitForSeconds delayStart = null)
1414
{
15-
return base.Tick(delayStart);
15+
yield return base.Tick(delayStart);
1616
}
1717
}
1818
}

0 commit comments

Comments
 (0)