diff --git a/Assets/Scripts/Input/HerdController.cs b/Assets/Scripts/Input/HerdController.cs index ff1f803..33b95ad 100644 --- a/Assets/Scripts/Input/HerdController.cs +++ b/Assets/Scripts/Input/HerdController.cs @@ -19,19 +19,27 @@ public class HerdController : MonoBehaviour [SerializeField] private float WaitTime; + [SerializeField] + private Transform Centre; + + private float lastTime; private List Herd; + private Vector2 recievedInput; void Start() { SpawnHerd(); - StartCoroutine(MoveRoutine()); } //Recieved movement input from player private void OnMovement(InputValue value) { Vector2 input = value.Get(); + if (input.magnitude > 0) + input = input.normalized; + Herd.ForEach(p => p.SetMovement(input)); + recievedInput = input; } @@ -50,7 +58,7 @@ public class HerdController : MonoBehaviour for (int i = 0; i < HerdCount; i++) { - + int SpawnAttempt = 0; @@ -62,16 +70,16 @@ public class HerdController : MonoBehaviour if (SpawnPoint != null) position += SpawnPoint.position; - if (SpawnPositionValid(position,rotation, bound)) + if (SpawnPositionValid(position, rotation, bound)) { GameObject newObject = Instantiate(Prefab, position, rotation, transform); - + Herd.Add(newObject.GetComponent()); break; } - + SpawnAttempt++; if (SpawnAttempt % 10 == 0) @@ -84,28 +92,32 @@ public class HerdController : MonoBehaviour - Debug.Log("Total Spawned: " + Herd.Count); + } + Debug.Log("Total Spawned: " + Herd.Count); } - public IEnumerator MoveRoutine() + public void FixedUpdate() { - foreach (PlayerController pc in Herd) + if (recievedInput.magnitude > 0 && lastTime + WaitTime < Time.time) { - pc.MoveObject(WaitTime); + Herd.ForEach(p => p.MoveObject(WaitTime)); + lastTime = Time.time; + } - yield return new WaitForSeconds(WaitTime); - StartCoroutine(MoveRoutine()); + if (Centre != null) + Centre.position = Herd.Aggregate(new Vector3(0, 0, 0), (s, v) => s + v.transform.position) / (float)Herd.Count; } - private bool SpawnPositionValid(Vector3 position,Quaternion rotation ,Bounds bound) + + private bool SpawnPositionValid(Vector3 position, Quaternion rotation, Bounds bound) { - Collider[] colliders = Physics.OverlapBox(position, bound.extents,rotation); + Collider[] colliders = Physics.OverlapBox(position, bound.extents, rotation); Debug.DrawLine(position, position + Vector3.up); - foreach(Collider col in colliders) + foreach (Collider col in colliders) { if (col.GetComponentInChildren()) return false; diff --git a/Assets/Scripts/Input/PlayerController.cs b/Assets/Scripts/Input/PlayerController.cs index c52619c..0175610 100644 --- a/Assets/Scripts/Input/PlayerController.cs +++ b/Assets/Scripts/Input/PlayerController.cs @@ -11,16 +11,21 @@ public class PlayerController : MonoBehaviour public GameObject model; public CharacterController cController; - private float speedMulitplier; + public float speedMulitplier; + private float randomizer; + private float directionRandmoizer; private Vector2 receivedInput; private Vector3 moveDirection = Vector3.zero; + private float moveDelta; + private float lastMoveTime; // Start is called before the first frame update void Start() { speedMulitplier = UnityEngine.Random.Range(0.8f, 1.2f); cController = GetComponent(); + randomizer = UnityEngine.Random.Range(0,10); } public void SetMovement(Vector2 input) @@ -30,9 +35,24 @@ public class PlayerController : MonoBehaviour public void UpdatePosition() { + moveDelta = Time.time - lastMoveTime; + lastMoveTime = Time.time; + + + if (receivedInput.magnitude == 0) + return; + float HorseX, HorseZ; HorseZ = receivedInput.y; HorseX = receivedInput.x; + if (Mathf.Abs(HorseZ) == Mathf.Min(Mathf.Abs(HorseZ), Mathf.Abs(HorseX))) + { + HorseZ += directionRandmoizer; + } + else + { + HorseX += directionRandmoizer; + } if (cController.isGrounded) { @@ -40,6 +60,8 @@ public class PlayerController : MonoBehaviour moveDirection *= walkSpeed * speedMulitplier; } + + moveDirection.y -= gravity * Time.deltaTime; cController.Move(moveDirection * Time.deltaTime); @@ -104,6 +126,9 @@ public class PlayerController : MonoBehaviour // Update is called once per frame void Update() { + + speedMulitplier = (Mathf.Sin(Time.time * UnityEngine.Random.Range(0.9f,1.1f) + randomizer) + 2); + directionRandmoizer = Mathf.Cos(Time.time + randomizer / 2) * 0.3f; UpdatePosition(); } }