Browse Source

Movement coroutine

master
NickFowler 4 years ago
parent
commit
57030321f9
2 changed files with 33 additions and 13 deletions
  1. +19
    -2
      Assets/Scripts/Input/HerdController.cs
  2. +14
    -11
      Assets/Scripts/Input/PlayerController.cs

+ 19
- 2
Assets/Scripts/Input/HerdController.cs View File

@ -16,9 +16,16 @@ public class HerdController : MonoBehaviour
[SerializeField] [SerializeField]
private Transform SpawnPoint; private Transform SpawnPoint;
private List<PlayerController> Herd;
[SerializeField]
private float WaitTime;
private List<PlayerController> Herd;
void Start()
{
SpawnHerd();
StartCoroutine(MoveRoutine());
}
//Recieved movement input from player //Recieved movement input from player
private void OnMovement(InputValue value) private void OnMovement(InputValue value)
@ -51,7 +58,7 @@ public class HerdController : MonoBehaviour
{ {
Vector3 position = Vector3.ProjectOnPlane(Random.onUnitSphere, Vector3.up) * radius; Vector3 position = Vector3.ProjectOnPlane(Random.onUnitSphere, Vector3.up) * radius;
Quaternion rotation = Quaternion.Euler(0, Random.Range(-25, 25), 0);
Quaternion rotation = Quaternion.identity;
if (SpawnPoint != null) if (SpawnPoint != null)
position += SpawnPoint.position; position += SpawnPoint.position;
@ -81,6 +88,16 @@ public class HerdController : MonoBehaviour
} }
} }
public IEnumerator MoveRoutine()
{
foreach (PlayerController pc in Herd)
{
pc.MoveObject();
}
yield return new WaitForSeconds(WaitTime);
StartCoroutine(MoveRoutine());
}
private bool SpawnPositionValid(Vector3 position,Quaternion rotation ,Bounds bound) private bool SpawnPositionValid(Vector3 position,Quaternion rotation ,Bounds bound)
{ {

+ 14
- 11
Assets/Scripts/Input/PlayerController.cs View File

@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
@ -11,7 +12,6 @@ public class PlayerController : MonoBehaviour
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
StartCoroutine((FlipOnAxis()));
} }
public void SetMovement(Vector2 input) public void SetMovement(Vector2 input)
@ -27,12 +27,15 @@ public class PlayerController : MonoBehaviour
float rotateTo = RotateObject(HorseX, HorseZ); float rotateTo = RotateObject(HorseX, HorseZ);
HorseZ *= Time.deltaTime * walkSpeed;
HorseX *= Time.deltaTime * walkSpeed;
HorseZ *= walkSpeed;
HorseX *= walkSpeed;
transform.Translate(HorseX, 0, HorseZ); transform.Translate(HorseX, 0, HorseZ);
LeanTween.rotateY(model, rotateTo, 0.1f);
//Vector3 dir = Quaternion.Euler(-90, rotateTo, 0) * Vector3.forward;
//model.transform.forward = dir;
//LeanTween.rotateY(model, rotateTo, 0.1f);
} }
@ -40,7 +43,7 @@ public class PlayerController : MonoBehaviour
{ {
float to = model.transform.rotation.eulerAngles.y; float to = model.transform.rotation.eulerAngles.y;
//Debug.Log(to); //Debug.Log(to);
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
if (receivedInput.x != 0 || receivedInput.y != 0)
{ {
if (zInput > 0 && xInput == 0) if (zInput > 0 && xInput == 0)
to = 0; to = 0;
@ -63,18 +66,18 @@ public class PlayerController : MonoBehaviour
return to; return to;
} }
public IEnumerator FlipOnAxis()
public void MoveObject()
{ {
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
UpdatePosition();
if (receivedInput.x != 0 || receivedInput.y != 0)
{ {
LeanTween.rotateX(model, -90, 0.1f);
model.transform.Rotate(-90, 0, 0);
} }
yield return new WaitForSeconds(0.5f);
} }
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
UpdatePosition();
} }
} }

Loading…
Cancel
Save