Hazkin

Untitled

Aug 2nd, 2023
1,312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.71 KB | None | 0 0
  1. private Vector3 _prevPosition;
  2. private int _bufferSize = 8;
  3. private int _bufferIndex = 0;
  4. private Vector3[] _buffer;
  5. [SerializeField] private Transform racket;
  6.  
  7. private void Start()
  8. {
  9.     _buffer = new Vector3[_bufferSize];
  10.     _bufferIndex = 0;
  11.     _prevPosition = racket.transform.position;
  12. }
  13.  
  14. private void Update()
  15. {
  16.     var currentPosition = racket.transform.position;
  17.     var delta = currentPosition - _prevPosition;
  18.     _buffer[_bufferIndex] = delta;
  19.     _bufferIndex = (_bufferIndex + 1) % _bufferSize;
  20.  
  21.     var velocity = GetRacketVelocity();
  22. }
  23.  
  24. private float GetRacketVelocity()
  25. {
  26.     Vector3 total = Vector3.zero;
  27.     for (int i = 0; i < _buffer.Length; i++)
  28.     {
  29.         total += _buffer[i];
  30.     }
  31.  
  32.     return total.magnitude;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment