Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class NewBehaviourScript : MonoBehaviour
- {
- [SerializeField] private Vector2 _a;
- [SerializeField] private Vector2 _b;
- [SerializeField] private float _aAngle = 45;
- [SerializeField] private float _bAngle;
- [SerializeField] private float _aMagnitude = 1;
- [SerializeField] private float _bMagnitude = 1;
- private void Update()
- {
- _a = Vector2Extension.GetRotatedVector(Vector2.right, _aAngle * Mathf.Deg2Rad) * _aMagnitude;
- _b = Vector2Extension.GetRotatedVector(Vector2.right, _bAngle * Mathf.Deg2Rad) * _bMagnitude;
- float dot = Vector2.Dot(_a, _b);
- float cos = dot / (_b.magnitude * _a.magnitude);
- float AonBProjection = dot / (_b.magnitude);
- Debug.DrawRay(transform.position, _a, Color.cyan);
- Debug.DrawRay(transform.position, _b, Color.magenta);
- Debug.DrawRay(transform.position + Vector3.down * 0.01f, _b.normalized * cos, Color.red);
- Debug.DrawRay(transform.position + Vector3.down * 0.02f, _b.normalized * AonBProjection, Color.green);
- Debug.DrawRay(transform.position + Vector3.down * 0.03f, _b.normalized * dot, Color.blue);
- Debug.DrawLine(Vector3.down * 0.02f + (Vector3)_b.normalized * AonBProjection, (Vector3)_a, Color.yellow);
- }
- }
- 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 = GetRotatedVector(r, angle);
- return (rotatedR - r).normalized;
- float GetRotationAngle(float r, float chord) =>
- Mathf.Asin(chord / (2 * r)) * 2;
- }
- public static Vector2 GetRotatedVector(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