Hello, I am attempting to create a list of Vector2 all a specific distance apart and at a random angle between 2 ints from one another. The way I intended to do this was simply using Debug.DrawLine(v2start, v2end), for debugging purposes and iterate through however many lines I wanted to create. However I am hitting a snag at rotating v2end at a specific angle from v2start, my code here: public Vector2 start; public Vector2 end; public float inc_size; - Pastebin.com — Most relevent code at line 39 and 21.
Please forgive some messiness, currently just toying with the idea, not super clean code.
Can’t you use Transform.Rotate
for this?
Can’t you use Transform.Rotate
for this?
Not gonna lie this took some digging, here is a nifty little function I put together for it however. All you have to do is set the size (the length of the line), angle, and starting point. It will then return a Vector2 that is set at that angle and that distance from the start Vector2.
…
If anyone has questions feel free to ask them below and I will try to get to them, would love to hear any questions or criticisms.
…
Rotate Vector2 around Vector2 code — Vector2 angle_calc(Vector2 start, float angle, float size) { f - Pastebin.com
…
Creating List of lines code — public List<Vector2> lines; void line_gen(int min_angle_mod, int max_an - Pastebin.com
If all you want to do is draw a line then why not let unity work it out for you?
Note that the same principal applies when working with Debug.DrawLine
. Why would you need to worry about working out the angles? Just plot the point positions of each intersection.
With regards to plotting the points using angles maybe brush up on your trigonometry because if I am reading your solution correctly you are brute forcing the solution which may be un-performant.
That said it should just me a matter of finding the length of two sides of the triangle you can use the law of sines to do this.
i.e.
public static class Vector3Extensions
{
public static Vector3 GetPositionForAngle(this Vector3 position, float angle, float magnitude)
{
var x = magnitude * Mathf.Sin(angle * Mathf.Deg2Rad);
var z = magnitude * Mathf.Cos(angle * Mathf.Deg2Rad);
return new Vector3(x, 0, z) + position;
}
}
public class AngleTester : MonoBehaviour
{
public LineRenderer LineRenderer;
void Start()
{
LineRenderer = GetComponent<LineRenderer>();
var point1 = Vector3.zero;
var point2 = point1 + (Vector3.forward * 5);
var point3 = point2.GetPositionForAngle(45, 5);
var point4 = point3.GetPositionForAngle(90, 5);
var points = new Vector3[] {
point1,
point2,
point3,
point4
};
LineRenderer.positionCount = points.Length;
LineRenderer.SetPositions(points);
}
}