Skip to content

Commit

Permalink
mom said i shouldn't use magic numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
keijiro committed Jan 30, 2018
1 parent 3daad69 commit b385955
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
65 changes: 52 additions & 13 deletions Assets/Puppet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@

public class Puppet : MonoBehaviour
{
[SerializeField] float _stepFrequency = 3;
[SerializeField] float _stride = 0.4f;
[SerializeField] float _stepHeight = 0.3f;
[SerializeField] float _bodyHeight = 0.9f;
[SerializeField] float _bodyUndulation = 10;
[SerializeField] Vector3 _handPosition = new Vector3(0.3f, 0.3f, -0.2f);
[SerializeField] float _handMove = 0.2f;
[SerializeField] float _headMove = 3;

Animator _animator;
NoiseGenerator _bodyNoise;

Expand All @@ -19,24 +28,54 @@ void Update()

void OnAnimatorIK(int layerIndex)
{
var footTime = Time.time * 4;
var footTime = Time.time * _stepFrequency;

{
var fx = _stride * 0.5f;
var fy = Mathf.Max(0, Mathf.Sin(footTime)) * _stepHeight;
_animator.SetIKPosition(AvatarIKGoal.LeftFoot, new Vector3(fx, fy, 0));
_animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1);
}

_animator.SetIKPosition(AvatarIKGoal.LeftFoot, new Vector3(0.2f, 0.3f * Mathf.Max(0, Mathf.Sin(footTime)), 0));
_animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1);
{
var fx = _stride * -0.5f;
var fy = Mathf.Max(0, Mathf.Sin(footTime + Mathf.PI)) * _stepHeight;
_animator.SetIKPosition(AvatarIKGoal.RightFoot, new Vector3(fx, fy, 0));
_animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1);
}

_animator.SetIKPosition(AvatarIKGoal.RightFoot, new Vector3(-0.2f, 0.3f * Mathf.Max(0, Mathf.Sin(footTime + Mathf.PI)), 0));
_animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1);
{
var bx = Mathf.Sin(footTime) * _stride * -0.5f;
var by = _bodyHeight + Mathf.Cos(footTime * 2) * _stepHeight / 3;
_animator.bodyPosition = new Vector3(bx, by, 0);
}

_animator.bodyPosition = new Vector3(Mathf.Sin(footTime) * -0.2f, 0.9f + Mathf.Cos(footTime * 2) * 0.1f, 0);
_animator.bodyRotation = Quaternion.AngleAxis(180, Vector3.up) * _bodyNoise.Rotation(1, 10, 10, 10);
{
var pivot = Quaternion.AngleAxis(180, Vector3.up);
var und = _bodyNoise.Rotation(1, _bodyUndulation);
_animator.bodyRotation = pivot * und;
}

_animator.SetIKPosition(AvatarIKGoal.LeftHand, _animator.bodyPosition + new Vector3(0.3f, 0.2f, -0.2f) + Vector3.Scale(_bodyNoise.Vector(2), new Vector3(0.1f, 0.3f, 0.1f)));
_animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);
{
var pos = _handPosition;
pos += _animator.bodyPosition + _bodyNoise.Vector(3) * _handMove;
_animator.SetIKPosition(AvatarIKGoal.LeftHand, pos);
_animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);
}

_animator.SetIKPosition(AvatarIKGoal.RightHand, _animator.bodyPosition + new Vector3(-0.3f, 0.2f, -0.2f) + Vector3.Scale(_bodyNoise.Vector(3), new Vector3(0.1f, 0.3f, 0.1f)));
_animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
{
var pos = _handPosition;
pos.x *= -1;
pos += _animator.bodyPosition + _bodyNoise.Vector(4) * _handMove;
_animator.SetIKPosition(AvatarIKGoal.RightHand, pos);
_animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
}

_animator.SetLookAtPosition(new Vector3(0, 0, -1) + _bodyNoise.Vector(4));
_animator.SetLookAtWeight(1);
{
var pos = _bodyNoise.Vector(5) * _headMove;
pos.z = -2;
_animator.SetLookAtPosition(pos);
_animator.SetLookAtWeight(1);
}
}
}
8 changes: 8 additions & 0 deletions Assets/Test/Test.unity
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 74eacbddd55ba8c47a31b3087d99f2b9, type: 3}
m_Name:
m_EditorClassIdentifier:
_stepFrequency: 3
_stride: 0.4
_stepHeight: 0.3
_bodyHeight: 0.9
_bodyUndulation: 10
_handPosition: {x: 0.3, y: 0.3, z: -0.2}
_handMove: 0.2
_headMove: 3
--- !u!1001 &1580182897
Prefab:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit b385955

Please sign in to comment.