I have been attempting to make a line originating from a point point in the direction of the mouse cursor. It also changes length depending on it’s distance from the cursor with a max length. The distance changing works by altering the scale of the lines transform and then moving it’s transform so the appropriate distance so the line is still originating from the same point. This is fully functional on it’s own and so is the rotation which I use a parent “pivot” game object for. However when combined it fully breaks with the transform position of the line notably being entirely incorrect with changes to the y value despite me having only changed the x value when adjusting the position of the line. While trying to figure out the source of it I made it so that instead of changing the x value to sprite.bounds.extents.x, I just kept it at a constant 1. In practice both the x and the y values were changing. I assume this has something to do with rotating and transforming the game object at the same time but I have no idea how to fix it. Thank you for any help.
Note: to describe the breaking of the line, while it will always point towards the cursor its movements are almost like there is a floor on the line x = 0 and it has physics and gravity pulling it towards it. (Sorry that was awful I cant think of any other way to explain it)
void Update()
{
Vector3 mousepos = Input.mousePosition;
mousepos = Camera.main.ScreenToWorldPoint(mousepos);
mousepos.z = -1;
Spot.position = mousepos;
Vector2 diff = pivot.position - mousepos;
float length = Mathf.Sqrt(Mathf.Pow(diff.x, 2) + Mathf.Pow(diff.y, 2));
if (diff.y < 0)
{
pivot.transform.eulerAngles = new(0, 0, Mathf.Asin(diff.x / length) * Mathf.Rad2Deg + 90);
}
else
{
pivot.transform.eulerAngles = new(0, 0, -(Mathf.Asin(diff.x / length) * Mathf.Rad2Deg + 90));
}
if (length > maxlength)
{
length = maxlength;
}
transform.position = new Vector3(sprite.bounds.extents.x, 0, 0); //Here was where I replaced it with 1
transform.localScale = new Vector2(length, 0.4f);
}