StressReceiver.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
  2. using UnityEngine;
  3. public class StressReceiver : MonoBehaviour
  4. {
  5. public float trauma;
  6. private float _trauma;
  7. private float _currMaxTrauma;
  8. private Vector3 _lastPosition;
  9. private float _size;
  10. private Vector3 _lastRotation;
  11. [Tooltip("Exponent for calculating the shake factor. Useful for creating different effect fade outs")]
  12. public float TraumaExponent = 1;
  13. [Tooltip("Maximum angle that the gameobject can shake. In euler angles.")]
  14. public Vector3 MaximumAngularShake = Vector3.one * 5;
  15. [Tooltip("Maximum translation that the gameobject can receive when applying the shake effect.")]
  16. public Vector3 MaximumTranslationShake = Vector3.one * .75f;
  17. private System.Action callBack;
  18. [ContextMenu("asdasda")]
  19. public void Test()
  20. {
  21. InduceStress(trauma,1,null);
  22. }
  23. private void Update()
  24. {
  25. float shake = Mathf.Pow(_trauma, TraumaExponent);
  26. shake /= _currMaxTrauma;
  27. // float shake = _trauma;
  28. /* Only apply this when there is active trauma */
  29. if(shake > 0.01f)
  30. {
  31. var previousRotation = _lastRotation;
  32. var previousPosition = _lastPosition;
  33. /* In order to avoid affecting the transform current position and rotation each frame we substract the previous translation and rotation */
  34. _lastPosition = new Vector3(
  35. MaximumTranslationShake.x * (Mathf.PerlinNoise(0, Time.time * 25) * 2 - 1),
  36. MaximumTranslationShake.y * (Mathf.PerlinNoise(1, Time.time * 25) * 2 - 1),
  37. MaximumTranslationShake.z * (Mathf.PerlinNoise(2, Time.time * 25) * 2 - 1)
  38. ) * shake*_size;
  39. _lastRotation = new Vector3(
  40. MaximumAngularShake.x * (Mathf.PerlinNoise(3, Time.time * 25) * 2 - 1),
  41. MaximumAngularShake.y * (Mathf.PerlinNoise(4, Time.time * 25) * 2 - 1),
  42. MaximumAngularShake.z * (Mathf.PerlinNoise(5, Time.time * 25) * 2 - 1)
  43. ) * shake*_size;
  44. transform.localPosition += _lastPosition - previousPosition;
  45. transform.localRotation = Quaternion.Euler(transform.localRotation.eulerAngles + _lastRotation - previousRotation);
  46. _trauma =_trauma - Time.deltaTime;
  47. }
  48. else
  49. {
  50. if (_lastPosition == Vector3.zero && _lastRotation == Vector3.zero)
  51. {
  52. callBack?.Invoke();
  53. return;
  54. }
  55. /* Clear the transform of any left over translation and rotations */
  56. transform.localPosition -= _lastPosition;
  57. transform.localRotation = Quaternion.Euler(transform.localRotation.eulerAngles - _lastRotation);
  58. _lastPosition = Vector3.zero;
  59. _lastRotation = Vector3.zero;
  60. callBack?.Invoke();
  61. }
  62. }
  63. /// <summary>
  64. /// Applies a stress value to the current object.
  65. /// </summary>
  66. /// <param name="Stress">[0,1] Amount of stress to apply to the object</param>
  67. public void InduceStress(float time,float size,System.Action callBack)
  68. {
  69. this._size = size;
  70. this.callBack = callBack;
  71. _trauma = time;
  72. _currMaxTrauma=time;
  73. }
  74. }