Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How do I create a laser effect with Boxcast2D?

Discussion in 'Scripting' started by billygamesinc, Aug 30, 2023.

  1. billygamesinc

    billygamesinc

    Joined:
    Dec 5, 2020
    Posts:
    245
    Code (CSharp):
    1.     private void Fire(Vector2 originPos){
    2.         Vector3 direction = originPos + (Vector2.right * _length);
    3.         _beamSprite.startWidth = _width;
    4.         _beamSprite.SetPosition(0,originPos);
    5.         _beamSprite.SetPosition(1,direction);
    6.         Gizmos.BoxCast(new Vector2(direction.x/2,direction.y),new Vector2(_length,_width),_angle,Vector2.zero,0,0);
    7.     }
    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.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
  3. billygamesinc

    billygamesinc

    Joined:
    Dec 5, 2020
    Posts:
    245
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    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.
     
  5. billygamesinc

    billygamesinc

    Joined:
    Dec 5, 2020
    Posts:
    245
    Thanks Ill give this a shot, do you think this is more performant then something like attaching a boxcollider and modifying its size?
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    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.
     
    billygamesinc likes this.
  7. billygamesinc

    billygamesinc

    Joined:
    Dec 5, 2020
    Posts:
    245
    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
    Code (CSharp):
    1.         Vector3 startPos = originPos + (Vector2.right * _range/2);
    2.         Gizmos.BoxCast(startPos,new Vector2(_range,_size),0,Vector2.zero,0,0);
     
    Last edited: Aug 30, 2023
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563