FreshRSS

Normální zobrazení

Jsou dostupné nové články, klikněte pro obnovení stránky.
PředevčíremHlavní kanál

How do make a box collider align with a line renderer continuously?

I am having the issue of aligning my BoxCollider2D with my linerenderer.

I am trying to have a laser shoot using:
Physics2D.Linecast(startingPoint.transform.position, maxEndPoint.transform.position);

This works fine and the line is always shooting in the right direction and angle when I rotate the object. But, when I am drawing a box collider, the box doesn't align at the correct position.

Firstly, the boxcollider is attached as a child component of the line renderer.

I am using this code to get the size of the collider, which seems to work fine.

float lineLength = Vector3.Distance(lazer.GetPosition(1), lazer.GetPosition(0)); 

This code is for aligning the collider with the line renderer. I know I am supposed to get the midpoint, because that's how you control the location of the box collider, but I need to know the math to always make it align.

    lazerCollider.offset = new Vector3(0, ourHitPos.y - startingPoint.transform.position.y, 0) / 2;
    lazerCollider.size = new Vector3(lazerWidth, lineLength, 0);

This is when the beam is vertical This is if the beam is rotated

I know right now I am only using the Y coordinate... I just need to know the math to make this work.

❌
❌