Comparing angle difference between 2 parts

  1. What do you want to achieve? Keep it simple and clear!
    I want a beam attack in my game that the player is able to fire and once it hits a wall or the floor it creates an explosion.

  2. What is the issue? Include screenshots / videos if possible!
    I only want the explosion to occur when the beam part and hit part have a certain difference in degrees. This is to prevent the beam to hit the ground to often when players are firing the beam.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have looked at :ToWorldSpace and other similar functions to get a relative but after testing it really doesn’t yield me the result I want. So how could I go about comparing the angles between these two parts so that the beam would only be able to hit the floor in this case when being fired forward?

You could use DotProduct for this.

local dot = Part1.CFrame.LookVector:Dot(Part2.CFrame.LookVector)

Basically, we get direction of one part, dot product it with other part’s direction and see if there’s a difference, for example:

if dot < 0.5 then
    -- do this
end

what

I took the absolute value and tried to compare it and I ended up with this.

Sorry for the late reply. I’m a little confused with the issue, may you please elaborate and send me your code?

You’ll probably want him to use the surface normal of the part rather than the look vector. I think that’s likely the issue.

Depending on the direction the beam is fired in Dot isn’t the same.

Tried looking into it but I don’t really understand it.

Sorry mate, I was at work. How does your system detect hits? Let’s start there.