TestObjectMovement.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using SchoenLogger;
  4. using SchoenLogger.Sample;
  5. using UnityEngine;
  6. namespace SchoenLogger.Sample
  7. {
  8. public class TestObjectMovement : MonoBehaviour
  9. {
  10. public StudyManager<SampleCondition> StudyManager;
  11. public float slowSpeed = 3;
  12. public float fastSpeed = 6;
  13. private SampleCondition currentCondition;
  14. private bool started = false;
  15. private Vector3 target = Vector3.zero;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. StudyManager.ChangeCondition.AddListener(SetupCondition);
  20. StudyManager.StartCondition.AddListener(StartExperiment);
  21. }
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. if (currentCondition == null || !started)
  26. return;
  27. if (Vector3.SqrMagnitude(target - transform.position) < 0.01f)
  28. {
  29. target.x = Random.Range(-5, 5);
  30. target.y = Random.Range(-5, 5);
  31. target.z = Random.Range(-5, 5);
  32. }
  33. switch (currentCondition.movement)
  34. {
  35. case SampleCondition.MovementType.randomMovement:
  36. transform.position = Vector3.MoveTowards(transform.position, target, Time.deltaTime * slowSpeed);
  37. break;
  38. case SampleCondition.MovementType.fastRandomMovement:
  39. transform.position = Vector3.MoveTowards(transform.position, target, Time.deltaTime * fastSpeed);
  40. break;
  41. }
  42. }
  43. private void StartExperiment(SampleCondition arg0, int arg1)
  44. {
  45. started = true;
  46. }
  47. void SetupCondition(SampleCondition cond, int partId)
  48. {
  49. currentCondition = cond;
  50. }
  51. }
  52. }