diff --git a/Assets/Scripts/Input/HerdController.cs b/Assets/Scripts/Input/HerdController.cs index 05a682e..33b95ad 100644 --- a/Assets/Scripts/Input/HerdController.cs +++ b/Assets/Scripts/Input/HerdController.cs @@ -19,6 +19,8 @@ public class HerdController : MonoBehaviour [SerializeField] private float WaitTime; + [SerializeField] + private Transform Centre; private float lastTime; private List Herd; @@ -27,7 +29,6 @@ public class HerdController : MonoBehaviour void Start() { SpawnHerd(); - StartCoroutine(MoveRoutine()); } //Recieved movement input from player @@ -37,7 +38,7 @@ public class HerdController : MonoBehaviour if (input.magnitude > 0) input = input.normalized; - Herd.ForEach(p => p.SetMovement(input)); + Herd.ForEach(p => p.SetMovement(input)); recievedInput = input; } @@ -57,7 +58,7 @@ public class HerdController : MonoBehaviour for (int i = 0; i < HerdCount; i++) { - + int SpawnAttempt = 0; @@ -69,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) @@ -91,7 +92,7 @@ public class HerdController : MonoBehaviour - + } Debug.Log("Total Spawned: " + Herd.Count); } @@ -100,33 +101,23 @@ public class HerdController : MonoBehaviour { if (recievedInput.magnitude > 0 && lastTime + WaitTime < Time.time) { - Herd.ForEach(p => p.MoveObject()); - Debug.Log("Time waited: " + (Time.time - lastTime)); + Herd.ForEach(p => p.MoveObject(WaitTime)); lastTime = Time.time; - - } - } - - - public IEnumerator MoveRoutine() - { - foreach (PlayerController pc in Herd) - { - pc.MoveObject(WaitTime); } - 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 d4603e4..167e1e3 100644 --- a/Assets/Scripts/Input/PlayerController.cs +++ b/Assets/Scripts/Input/PlayerController.cs @@ -9,13 +9,17 @@ public class PlayerController : MonoBehaviour public float walkSpeed; public GameObject model; - private float speedMulitplier; + public float speedMulitplier; + private float randomizer; + private float directionRandmoizer; private Vector2 receivedInput; + private float moveDelta; + private float lastMoveTime; // Start is called before the first frame update void Start() { - speedMulitplier = UnityEngine.Random.Range(0.8f, 1.2f); + randomizer = UnityEngine.Random.Range(0,10); } public void SetMovement(Vector2 input) @@ -25,14 +29,26 @@ 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; + float rotateTo = RotateObject(HorseX, HorseZ); - HorseZ *= Time.deltaTime * walkSpeed * speedMulitplier; - HorseX *= Time.deltaTime * walkSpeed * speedMulitplier; + HorseZ *= walkSpeed * speedMulitplier * moveDelta; + HorseX *= walkSpeed * speedMulitplier * moveDelta; transform.Translate(HorseX, 0, HorseZ); @@ -77,15 +93,19 @@ public class PlayerController : MonoBehaviour public IEnumerator RandomWait(float wait) { - yield return new WaitForSeconds(UnityEngine.Random.Range(0, wait/2)); + yield return new WaitForSeconds(UnityEngine.Random.Range(0, wait * randomizer/10)); Vector3 rotateDir = new Vector3(90 * Math.Sign(receivedInput.y), 0, -90 * Math.Sign(receivedInput.x)); model.transform.Rotate(rotateDir, Space.World); + } // 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(); } } diff --git a/Assets/WorldAssets/Prefabs/Horse.prefab b/Assets/WorldAssets/Prefabs/Horse.prefab index 531d448..b1708a4 100644 --- a/Assets/WorldAssets/Prefabs/Horse.prefab +++ b/Assets/WorldAssets/Prefabs/Horse.prefab @@ -45,8 +45,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 8f7fbcfa6410c204e8977bb70e5ff84b, type: 3} m_Name: m_EditorClassIdentifier: - walkSpeed: 2 + walkSpeed: 10 model: {fileID: 7954727171190983878} + speedMulitplier: 0 --- !u!143 &7371939184051845555 CharacterController: m_ObjectHideFlags: 0