Griff1th

Vector2Extension

Mar 15th, 2023 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | Gaming | 0 0
  1. using UnityEngine;
  2.  
  3. public static class Vector2Extension
  4. {
  5.     public static Vector2 GetDirectionAlongCircle(Vector2 targetPositioin, Vector2 selfPoisition, float speed)
  6.     {
  7.         Vector2 r = selfPoisition - targetPositioin;
  8.         float angle = GetRotationAngle(r.magnitude, speed);
  9.         Vector2 rotatedR = RotateVector(r, angle);
  10.         return (rotatedR - r).normalized;      
  11.  
  12.         float GetRotationAngle(float r, float chord) =>
  13.             Mathf.Asin(chord / (2 * r)) * 2;
  14.     }
  15.     public static Vector2 RotateVector(Vector2 vector, float rad)
  16.     {
  17.         Vector2 right = new Vector2(Mathf.Cos(rad), Mathf.Sin(rad));
  18.         Vector2 up = new Vector2(-Mathf.Sin(rad), Mathf.Cos(rad));
  19.         return new Vector2(right.x * vector.x + up.x * vector.y,
  20.                            right.y * vector.x + up.y * vector.y);
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment