How do I create a laser effect with Boxcast2D?

    private void Fire(Vector2 originPos){
        Vector3 direction = originPos + (Vector2.right * _length);
        _beamSprite.startWidth = _width;
        _beamSprite.SetPosition(0,originPos);
        _beamSprite.SetPosition(1,direction);
        Gizmos.BoxCast(new Vector2(direction.x/2,direction.y),new Vector2(_length,_width),_angle,Vector2.zero,0,0);
    }

I am trying to create a beam effect where all targets inside the beam take damage. However, I’m having trouble getting it to line up with the beams position. Should I use a collider instead? The beam’s width and length is dyanmic so I will need to adjust it with code.

A linecast might make more sense here: Unity - Scripting API: Physics.Linecast

The issue is I can’t modify the thickness of the linecast.

In hindsight, there’s no ‘LinecastAll’ method, so perhaps not very useful. There is a BoxcastAll though.

For the box cast, it’s pretty simple to work out the parameters you need. You just need to know the center position - which is the start position + half the direction to the end position - and the the ‘half-extends’, which is just half of what will be the overall size of the box. The length is similar to before, half the direction/distance to the end point. And then the width and height are just half whatever size you want to define.

Thanks Ill give this a shot, do you think this is more performant then something like attaching a boxcollider and modifying its size?

Using a trigger collider is an option. Performance is probably negligible. You’d have to measure that (but only if you are experiencing performance issues).

In either case you will still need to work out the position and size of the box. But the same logic I have above should work for both of them, except in the box collider you need to work out a rotation based on the direction, rather than just a direction.

What is the formula for this? I’m trying to figure this part out at the moment.
I think I have it actually it’s

        Vector3 startPos = originPos + (Vector2.right * _range/2);
        Gizmos.BoxCast(startPos,new Vector2(_range,_size),0,Vector2.zero,0,0);

Not sure what you mean by formula… did you see the visualizer I just made in your other thread?

https://forum.unity.com/threads/calculating-offset-for-rotation-pivot-of-this-boxcast.1486344/#post-9261741

The testing code goes back and forth between start and finish and direction and angle and keeps it all straight. See the video in there.