using System.Collections; using System.Collections.Generic; using UnityEngine; public class Walk : MonoBehaviour { public List legs = new List(); private List legsInitial = new List(); // Use this for initialization void Start() { for (int index = 0; index < legs.Count; ++index) { legsInitial.Add(legs[index].GetComponent().point); step2.Add( new Vector3() ); } } struct step { public Vector3 pos; public bool inAir; public step(Vector3 p, bool i) { pos = p; inAir = i; } } // 1, 3, 5 -= 3 int offset(int i, int offset, int max) { return i - offset < 0 ? (max - (offset - i)) : i - offset; } step[] s = { new step(new Vector3(-1, 0, 0), false), new step(new Vector3(0, -2, 0), false), new step(new Vector3(0, 0, 0), false), // rest new step(new Vector3(1, 0, 0), false), new step(new Vector3(1, 0, 0), false), new step(new Vector3(0, 0, 0), false), // rest new step(new Vector3(0, 2, 0), false), new step(new Vector3(-1, 0, 0), true), }; public int st = 5; public int speed2 = 30; int speed = 10; public float rotation = 0; int size = 8; public float walkspeed = 1; private List step2 = new List(); // Update is called once per frame void FixedUpdate() { if (speed <= 0) { if (st >= size-1) st = 0; else st++; for (int index = 0; index < legs.Count; ++index) { int t = st; if (index == 1 || index == 3 || index == 5) { t = offset(st, 4, size); } if (s[t].inAir == false) { Vector3 t2 = s[t].pos; t2.x *= walkspeed; // rotate x,y around rotation. step2[index] = (Quaternion.Euler(0, rotation, 0) * t2) / speed2; } else { Vector3 t2 = (legsInitial[index] + s[t].pos) - legs[index].GetComponent().point; step2[index] = t2 / speed2; } } speed = speed2; } else { speed--; makestep(); } } void makestep() { for (int index = 0; index < legs.Count; ++index) { legs[index].GetComponent().point += step2[index]; } } }