DirectionIndicator.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Receive
  6. {
  7. public class DirectionIndicator : MonoBehaviour {
  8. public Material darkColor;
  9. public Material brightColor;
  10. public string direction;
  11. public Receive messenger;
  12. public GameObject target;
  13. public Vector3 pointerPosition;
  14. private Camera cam;
  15. float transitionSpeed = 0.001f;
  16. float lerpFactor = 0;
  17. int decreaseOrIncrease = 1;
  18. float maxFlashRate = 0.3f;
  19. float minFlashRate = 0.05f;
  20. float distanceMultiplyer = 0.2f;
  21. public int xAxis = 0;
  22. public int yAxis = 1;
  23. float minimumTreshhold = 0.8f;
  24. public bool currentlyActive = true;
  25. Material thisMaterial;
  26. int frameCounter = 0;
  27. int vibrationIntervall = 12;
  28. public int frameOffset;
  29. bool onRightTarget = false;
  30. float positionTreshhold = 0.7f;
  31. public Vector3 crossCenter;
  32. public float flashRate = -1;
  33. // Use this for initialization
  34. void Start () {
  35. thisMaterial = GetComponent<Renderer> ().material;
  36. cam = Camera.main;
  37. }
  38. void Update () {
  39. frameCounter++;
  40. if (frameCounter >= frameOffset + vibrationIntervall * 2)
  41. {
  42. frameCounter = 0;
  43. }
  44. /*if (transform.position.x < -positionTreshhold + crossCenter.x)
  45. {
  46. xAxis = -1;
  47. }
  48. else if (transform.position.x > positionTreshhold + crossCenter.x)
  49. {
  50. xAxis = 1;
  51. }
  52. else
  53. {
  54. xAxis = 0;
  55. }
  56. if (transform.position.y < -positionTreshhold + crossCenter.y)
  57. {
  58. yAxis = -1;
  59. }
  60. else if (transform.position.y > positionTreshhold + crossCenter.y)
  61. {
  62. yAxis = 1;
  63. }
  64. else
  65. {
  66. yAxis = 0;
  67. }*/
  68. if (target != null && currentlyActive && getFlashRate() >= 0) {
  69. transitionSpeed = Mathf.Min (Mathf.Max (getFlashRate (), minFlashRate),maxFlashRate);
  70. lerpFactor += decreaseOrIncrease * transitionSpeed;
  71. //setMotorActive();
  72. if (lerpFactor > 1f) {
  73. decreaseOrIncrease = -1;
  74. } else if (lerpFactor < 0f) {
  75. decreaseOrIncrease = 1;
  76. }
  77. } else {
  78. lerpFactor = 0f;
  79. //messenger.setMotor (direction, false);
  80. }
  81. thisMaterial.Lerp (darkColor, brightColor, lerpFactor);
  82. }
  83. float getFlashRate(){
  84. /*float result;
  85. //Vector3 pointerCoordinates = cam.ScreenToWorldPoint(new Vector3( Input.mousePosition.x,Input.mousePosition.y,0));
  86. Vector3 pointerCoordinates = pointerPosition;
  87. bool rightXPosition = ( Mathf.Abs ((target.transform.position.x - pointerCoordinates.x)) <= minimumTreshhold);
  88. bool rightYPosition = ( Mathf.Abs((target.transform.position.y - pointerCoordinates.y)) <= minimumTreshhold);
  89. if (rightXPosition && rightYPosition) {
  90. result = maxFlashRate;
  91. onRightTarget = true;
  92. }
  93. else if((xAxis != 0 && rightXPosition) || (yAxis != 0 && rightYPosition)){
  94. result = -1;
  95. onRightTarget = false;
  96. }
  97. else {
  98. result = distanceMultiplyer * (1/ ((xAxis*( target.transform.position.x - pointerCoordinates.x )) +
  99. yAxis*( target.transform.position.y - pointerCoordinates.y )));
  100. onRightTarget = false;
  101. }
  102. */
  103. return flashRate;
  104. }
  105. public void setFlash(bool b) {
  106. if (b) {
  107. flashRate = maxFlashRate * 0.3f;
  108. }
  109. else
  110. {
  111. flashRate = -1;
  112. }
  113. }
  114. void setMotorActive()
  115. {
  116. if (frameCounter >= frameOffset + vibrationIntervall || onRightTarget)
  117. {
  118. messenger.setMotor(direction, true);
  119. }
  120. else
  121. {
  122. messenger.setMotor(direction, false);
  123. }
  124. }
  125. }
  126. }