Hello devs!
I doing bullets script for my game and i wanted to do rickoshet when degrees between old and new vectors is >45 but i got a problem, i tryed to find some formules about this and i find it but this dont work for me, maybe i doing something wrong?
function understandRickoshet(Vector,Normal)
local NewVector = (Vector-(2*Vector:Dot(Normal)*Normal))
local GradusesBeetwenVectors = 180-math.deg(math.acos(((Vector.X*NewVector.X)+(Vector.Y*NewVector.Y)+(Vector.Z*NewVector.Z))/(math.sqrt(Vector.X^2+Vector.Y^2+Vector.Z^2)*math.sqrt(NewVector.X^2+NewVector.Y^2+NewVector.Z^2))))
if GradusesBeetwenVectors > 45 then
print("rickoshet")
end
end
Numbers which this function returns sometimes looks like true but then its brokes.
Sorry for my grammar
-- I'm assuming Normal is already a unit vector
local NewVector = Vector - 2*Vector:Dot(Normal)*Normal
local angle = math.acos(Vector.Unit:Dot(Normal)) -- in radians
Also, you’re checking if the angle is <45 degrees because of your 180- part, is that what you want? I would think that ricochets happen when the reflection and the incidence are large angles:
if math.deg(angle) > 45 then
print("Ricochet")
end
function understandRickoshet(Vector,Normal)
local NewVector = Vector - 2*Vector:Dot(Normal)*Normal
local GradusesBeetwenVectors = math.deg(math.acos(Vector.Unit:Dot(Normal)))
if GradusesBeetwenVectors>45 then
script.ScreenGui.TextButton.Text = "Rickoshet"
else
script.ScreenGui.TextButton.Text = "No Rickoshet"
end
end
I cann this function when bullet hit something, Vector that’s bullet vector at hit moment and Normal is vector’s normal
CurObj,CurEndPos,CurNormal,CurMaterial = workspace:FindPartOnRayWithWhitelist(Ray.new(HP+Bullet.Vector*Bullet.PenetrationPower,-Bullet.Vector*Bullet.PenetrationPower),{HO})
if CurObj then
understandRickoshet(Bullet.Vector,CurNormal)
end
local angle = math.acos(Vector.Unit:Dot(NewVector.Unit))
But that would just cut the angle in half it wouldn’t break it.
Maybe the bullet is just penetrating the wall when you’re looking head-on and the raycast is skipped?
I tested this code which raycasts from the camera to the mouse and shows the angle and it seems to be working like I expect:
Expand to see code I used
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = game.Players.LocalPlayer:GetMouse()
local label = player.PlayerGui.ScreenGui.TextLabel
local function understandRickoshet(Vector,Normal)
local NewVector = Vector - 2*Vector:Dot(Normal)*Normal
local GradusesBeetwenVectors = math.deg(math.acos(Vector.Unit:Dot(NewVector.Unit)))
label.Text = string.format("%d", GradusesBeetwenVectors)
end
game:GetService("RunService").Stepped:Connect(function()
local ray = Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction * 999)
local CurObj,CurEndPos,CurNormal,CurMaterial = workspace:FindPartOnRayWithIgnoreList(ray, {character})
if CurObj then
understandRickoshet(ray.Direction,CurNormal)
end
end)
Vectors are close to aligned when I just graze the surface: