Browse Source

Made heard more organic

master
Joshua Reason 4 years ago
parent
commit
d60dd7c70d
3 changed files with 42 additions and 30 deletions
  1. +15
    -24
      Assets/Scripts/Input/HerdController.cs
  2. +25
    -5
      Assets/Scripts/Input/PlayerController.cs
  3. +2
    -1
      Assets/WorldAssets/Prefabs/Horse.prefab

+ 15
- 24
Assets/Scripts/Input/HerdController.cs View File

@ -19,6 +19,8 @@ public class HerdController : MonoBehaviour
[SerializeField] [SerializeField]
private float WaitTime; private float WaitTime;
[SerializeField]
private Transform Centre;
private float lastTime; private float lastTime;
private List<PlayerController> Herd; private List<PlayerController> Herd;
@ -27,7 +29,6 @@ public class HerdController : MonoBehaviour
void Start() void Start()
{ {
SpawnHerd(); SpawnHerd();
StartCoroutine(MoveRoutine());
} }
//Recieved movement input from player //Recieved movement input from player
@ -37,7 +38,7 @@ public class HerdController : MonoBehaviour
if (input.magnitude > 0) if (input.magnitude > 0)
input = input.normalized; input = input.normalized;
Herd.ForEach(p => p.SetMovement(input));
Herd.ForEach(p => p.SetMovement(input));
recievedInput = input; recievedInput = input;
} }
@ -57,7 +58,7 @@ public class HerdController : MonoBehaviour
for (int i = 0; i < HerdCount; i++) for (int i = 0; i < HerdCount; i++)
{ {
int SpawnAttempt = 0; int SpawnAttempt = 0;
@ -69,16 +70,16 @@ public class HerdController : MonoBehaviour
if (SpawnPoint != null) if (SpawnPoint != null)
position += SpawnPoint.position; position += SpawnPoint.position;
if (SpawnPositionValid(position,rotation, bound))
if (SpawnPositionValid(position, rotation, bound))
{ {
GameObject newObject = Instantiate(Prefab, position, rotation, transform); GameObject newObject = Instantiate(Prefab, position, rotation, transform);
Herd.Add(newObject.GetComponent<PlayerController>()); Herd.Add(newObject.GetComponent<PlayerController>());
break; break;
} }
SpawnAttempt++; SpawnAttempt++;
if (SpawnAttempt % 10 == 0) if (SpawnAttempt % 10 == 0)
@ -91,7 +92,7 @@ public class HerdController : MonoBehaviour
} }
Debug.Log("Total Spawned: " + Herd.Count); Debug.Log("Total Spawned: " + Herd.Count);
} }
@ -100,33 +101,23 @@ public class HerdController : MonoBehaviour
{ {
if (recievedInput.magnitude > 0 && lastTime + WaitTime < Time.time) 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; 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); Debug.DrawLine(position, position + Vector3.up);
foreach(Collider col in colliders)
foreach (Collider col in colliders)
{ {
if (col.GetComponentInChildren<PlayerController>()) if (col.GetComponentInChildren<PlayerController>())
return false; return false;

+ 25
- 5
Assets/Scripts/Input/PlayerController.cs View File

@ -9,13 +9,17 @@ public class PlayerController : MonoBehaviour
public float walkSpeed; public float walkSpeed;
public GameObject model; public GameObject model;
private float speedMulitplier;
public float speedMulitplier;
private float randomizer;
private float directionRandmoizer;
private Vector2 receivedInput; private Vector2 receivedInput;
private float moveDelta;
private float lastMoveTime;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
speedMulitplier = UnityEngine.Random.Range(0.8f, 1.2f);
randomizer = UnityEngine.Random.Range(0,10);
} }
public void SetMovement(Vector2 input) public void SetMovement(Vector2 input)
@ -25,14 +29,26 @@ public class PlayerController : MonoBehaviour
public void UpdatePosition() public void UpdatePosition()
{ {
moveDelta = Time.time - lastMoveTime;
lastMoveTime = Time.time;
if (receivedInput.magnitude == 0)
return;
float HorseX, HorseZ; float HorseX, HorseZ;
HorseZ = receivedInput.y; HorseZ = receivedInput.y;
HorseX = receivedInput.x; 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); 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); transform.Translate(HorseX, 0, HorseZ);
@ -77,15 +93,19 @@ public class PlayerController : MonoBehaviour
public IEnumerator RandomWait(float wait) 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)); Vector3 rotateDir = new Vector3(90 * Math.Sign(receivedInput.y), 0, -90 * Math.Sign(receivedInput.x));
model.transform.Rotate(rotateDir, Space.World); model.transform.Rotate(rotateDir, Space.World);
} }
// Update is called once per frame // Update is called once per frame
void Update() 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(); UpdatePosition();
} }
} }

+ 2
- 1
Assets/WorldAssets/Prefabs/Horse.prefab View File

@ -45,8 +45,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 8f7fbcfa6410c204e8977bb70e5ff84b, type: 3} m_Script: {fileID: 11500000, guid: 8f7fbcfa6410c204e8977bb70e5ff84b, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
walkSpeed: 2
walkSpeed: 10
model: {fileID: 7954727171190983878} model: {fileID: 7954727171190983878}
speedMulitplier: 0
--- !u!143 &7371939184051845555 --- !u!143 &7371939184051845555
CharacterController: CharacterController:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

Loading…
Cancel
Save