- Home /
This question was
closed Nov 01, 2021 at 08:42 PM by
$$anonymous$$ for the following reason:
The question is answered, right answer was accepted
Question by
$$anonymous$$ · Nov 01, 2021 at 12:14 PM ·
mathvectorsrotations
Rotate and proportionally scale a vector
Hello, how to rotate a vector and scale its magnitude proportional to the angle to get a for example constant value on the y-axis?
1.png
(4.4 kB)
Comment
Best Answer
Answer by andrew-lukasik · Nov 01, 2021 at 06:12 PM
A pani od matematyki nie uczyła jak liczyć miejsce zerowe funkcji, hm?
using UnityEngine;
public class MiejsceZeroweFunkcjiLiniowej : MonoBehaviour
{
[SerializeField] Vector2 _In = Vector2.one;
[SerializeField] float _InRotation = 0;// degrees
[SerializeField] Vector2 _Out;
[SerializeField] float _yCutoff = 1.5f;
void OnValidate ()
{
float inVecAngle = Mathf.Atan2( _In.y , _In.x );// radians
float inVecAngleRotated = inVecAngle + Mathf.Deg2Rad*_InRotation;// radians
Vector2 inVecRotated = new Vector2{ x=Mathf.Cos(inVecAngleRotated) , y=Mathf.Sin(inVecAngleRotated) };// unit vector, for simplicity
float fx = inVecRotated.y / inVecRotated.x;// f(x) = (vec.y/vec.x)*x + 0
float fy = inVecRotated.x / inVecRotated.y;// f(y) = (vec.x/vec.y)*y + 0
_Out = new Vector2{ x=fy*_yCutoff , y=_yCutoff };
}
void OnDrawGizmos ()
{
Gizmos.color = Color.yellow; Gizmos.DrawLine( new Vector3{x=-10,y=_yCutoff} , new Vector3{x=+10,y=_yCutoff} );
Gizmos.color = Color.white; Gizmos.DrawRay( Vector3.zero , _In );
Gizmos.color = Color.red; Gizmos.DrawRay( Vector3.zero , _Out );
}
}
Follow this Question
Related Questions
Find Vector3 perpendicular to Vector3 A in direction of Vector3 B 1 Answer
Calculate collision between 2 rotated boxes without using BoxCollider (MATH) 2 Answers
Result of subtracton vectors 1 Answer
How would you fake air resistance? (c#) 2 Answers
How to get a weighted random Vector2 between 3 points 1 Answer