Skip to content

Commit

Permalink
better way to determine destinations
Browse files Browse the repository at this point in the history
  • Loading branch information
keijiro committed Feb 1, 2018
1 parent a76312f commit 06e878f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
16 changes: 11 additions & 5 deletions Assets/Puppet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public class Puppet : MonoBehaviour
int _stepCount;
float _stepTime;

Vector3 GetNextStepPos(Vector3 pivot, Vector3 prev)
{
var dest = new Vector3(Random.value * 2 - 1, 0, Random.value * 2 - 1);
var dot = Vector3.Dot((prev - pivot).normalized, (dest - pivot).normalized);
var flip = dot < 0 ? -1 : 1;
return pivot + (dest - pivot).normalized * _stride * flip;
}

void Start()
{
_animator = GetComponent<Animator>();
Expand All @@ -43,17 +51,15 @@ void Update()

if (_stepTime > 1)
{
var dest = new Vector3(Random.value * 2 - 1, 0, Random.value * 2 - 1);

if ((_stepCount & 1) == 0)
{
_rightFootPos = _newRightFootPos;
_newRightFootPos = _newLeftFootPos + (dest - _newLeftFootPos).normalized * _stride;
_newRightFootPos = GetNextStepPos(_newLeftFootPos, _rightFootPos);
}
else
{
_leftFootPos = _newLeftFootPos;
_newLeftFootPos = _newRightFootPos + (dest - _newRightFootPos).normalized * _stride;
_newLeftFootPos = GetNextStepPos(_newRightFootPos, _leftFootPos);
}

_stepCount += 1;
Expand Down Expand Up @@ -87,7 +93,7 @@ void OnAnimatorIK(int layerIndex)
(rad > Mathf.PI && rad < Mathf.PI * 1.5f) ? _rightFootPos : _newRightFootPos,
Mathf.Sin(rad) * 0.5f + 0.5f
);
var up = Vector3.up * (_bodyHeight + Mathf.Cos(rad * 2) * _stepHeight / 3);
var up = Vector3.up * (_bodyHeight + Mathf.Cos(rad * 2) * _stepHeight / 2);
_animator.bodyPosition = pos + up;
}

Expand Down
12 changes: 6 additions & 6 deletions Assets/Test/Test.unity
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,14 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 74eacbddd55ba8c47a31b3087d99f2b9, type: 3}
m_Name:
m_EditorClassIdentifier:
_stepFrequency: 2
_stride: 0.4
_stepHeight: 0.3
_stepFrequency: 1.5
_stride: 0.45
_stepHeight: 0.2
_bodyHeight: 0.9
_bodyUndulation: 40
_handPosition: {x: 0.3, y: 0.3, z: 0.2}
_bodyUndulation: 30
_handPosition: {x: 0.3, y: 0.2, z: 0.25}
_handMove: 0.3
_headMove: 3
_headMove: 8
--- !u!1001 &1580182897
Prefab:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit 06e878f

Please sign in to comment.