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]
private float WaitTime;
[SerializeField]
private Transform Centre;
private float lastTime;
private List<PlayerController> 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<PlayerController>());
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<PlayerController>())
return false;

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

@ -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();
}
}

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

@ -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

Loading…
Cancel
Save