Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public static class Vector2Extension
- {
- public static Vector2 GetDirectionAlongCircle(Vector2 targetPositioin, Vector2 selfPoisition, float speed)
- {
- Vector2 r = selfPoisition - targetPositioin;
- float angle = GetRotationAngle(r.magnitude, speed);
- Vector2 rotatedR = RotateVector(r, angle);
- return (rotatedR - r).normalized;
- float GetRotationAngle(float r, float chord) =>
- Mathf.Asin(chord / (2 * r)) * 2;
- }
- public static Vector2 RotateVector(Vector2 vector, float rad)
- {
- Vector2 right = new Vector2(Mathf.Cos(rad), Mathf.Sin(rad));
- Vector2 up = new Vector2(-Mathf.Sin(rad), Mathf.Cos(rad));
- return new Vector2(right.x * vector.x + up.x * vector.y,
- right.y * vector.x + up.y * vector.y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment