Skip to content

Instantly share code, notes, and snippets.

@grifdail
Last active March 22, 2018 13:14
Show Gist options
  • Save grifdail/98e4b73f50b7341eae72e97711d4bfc7 to your computer and use it in GitHub Desktop.
Save grifdail/98e4b73f50b7341eae72e97711d4bfc7 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed = 10;
public float frequency = 1;
public FootTarget leftFoot;
public FootTarget rightFoot;
public float speedInfluence;
public float scaleLegHigh = 1;
public float lengthStep = 2;
public float ratioAnim = 0.3f;
float distance = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var axes = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
Vector3 delta = axes * speed;
transform.position += delta * Time.deltaTime;
var power = Vector3.Dot(transform.forward, axes.normalized);
transform.forward = axes;
var influencedTravel = delta.magnitude * Mathf.Clamp01(1 - delta.magnitude / speed * speedInfluence) * power;
distance += Time.deltaTime * influencedTravel;
if (delta.magnitude>0.5f) {
UpdateFoot(distance, 1+influencedTravel, leftFoot);
UpdateFoot(distance + frequency * 0.5f, influencedTravel, rightFoot);
} else {
SetFoot(leftFoot.item, GetFootPosition(leftFoot.offset), GetFootPosition(leftFoot.origin));
leftFoot.previousPoint = GetFootPosition(leftFoot.offset);
leftFoot.endPoint = GetFootPosition(leftFoot.offset);
SetFoot(rightFoot.item, GetFootPosition(rightFoot.offset), GetFootPosition(rightFoot.origin));
rightFoot.previousPoint = GetFootPosition(rightFoot.offset);
rightFoot.endPoint = GetFootPosition(rightFoot.offset);
}
}
void UpdateFoot(float distance, float forward, FootTarget target) {
var alpha = distance / frequency;
var currentCount = Mathf.Floor(alpha);
if (currentCount!= target.count) {
target.previousPoint = target.endPoint;
target.endPoint = GetFootPosition(target.offset + Vector3.forward * forward * lengthStep*(1+ratioAnim));
}
var t = Mathf.Clamp01(Mathf.InverseLerp(0, ratioAnim, alpha%1));
SetFoot(target.item, Vector3.Lerp(target.previousPoint, target.endPoint, t) + Vector3.up * Mathf.Sin(t*Mathf.PI) * scaleLegHigh, GetFootPosition(target.origin));
target.count = currentCount;
}
void SetFoot(Transform item, Vector3 end, Vector3 start) {
var dir = (start - end).normalized;
var scale = (start - end).magnitude;
var midpoint = (start + end) * 0.5f;
item.position = midpoint;
item.localRotation = Quaternion.LookRotation(dir, Vector3.up);
item.localScale = new Vector3(0.3f, 0.3f, scale);
}
Vector3 GetFootPosition(Vector3 offset) {
return transform.position + transform.TransformVector(offset);
}
[System.Serializable]
public class FootTarget {
public Vector3 offset;
public Vector3 origin;
public float count;
public Transform item;
public Vector3 endPoint;
public Vector3 previousPoint;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment