How do i get the straight angle for DotProduct?

So, i am trying to make a ‘‘straight angle’’ so if the object is in the a straight view in the dotproduct it will print ‘‘straight’’, is this possible?

Could you please elaborate on what exactly you are referring to with “straight angle” and “straight view”? Are you trying to check if two objects are facing the same direction?

so basically like if its not right or left, if its like mostly close straightly then print ‘‘straight’’

if lookvector:Dot(targetVector) == 1 then
    print("Straight")
end

the result of a dot product will be 1 if both unit vectors are facing the same direction.

You can get the angle difference with arc cosine like so, keep in mind the result might be in radians.

local angle = math.acos(lookvector:Dot(targetVector))

if angle == 0 then
    print("Straight!")
end
1 Like

you sure? then it will be 360 angles, but i haven’t try. i will reply if it works or not.

Summary

hopefully it does

In addition to what @gertkeno already said:
The dot product returns a value between -1 and 1. If the vectors face the same directions the result will be 1 and -1 if they are facing away from each other.


while wait(0.001) do
wait(0.1)

local PathfindingService = game:GetService("PathfindingService")
local TweenService = game:GetService("TweenService")

-- Variables for the car and destination, etc.
local carprimary = script.Parent
script.Parent.Throttle = 1

local path = PathfindingService:CreatePath()
path:ComputeAsync(carprimary.Position, workspace.Location.Position)

local waypoints = path:GetWaypoints()

local Debris = game:GetService("Debris")

for _, waypoint in pairs(waypoints) do
	
	local part = Instance.new("Part")
	part.Shape = "Ball"
	part.Material = "Neon"
	part.Name = "Pathfind_Point"	
	part.Size = Vector3.new(0.2, 0.2, 0.2)
	part.Position = waypoint.Position
	part.Anchored = true
	part.CanCollide = false
	part.Parent = game.Workspace
	local LocationFinder = script.Parent.Parent.Chassis2.CFrame.LookVector
	local LocationFinder_Location = (workspace.Location.Position - part.Position).Unit
	local dot = LocationFinder_Location:Dot(LocationFinder)
	Debris:AddItem(part, 0.01)
	if dot < 0.495 then
		script.Parent.Steer = -1
		print('left')
	elseif dot > 0.505 then
		script.Parent.Steer = 1
		print('right')	
	
	end
	
	--carprimary:MoveTo(waypoint.Position)
	wait(0.01)
end
wait(0.001)

end.

making it more easier tbh

I’ve updated my post for how to get the angle, dot products are a relationship of the angles but not the actual value. Furthermore you shouldn’t actually use == 1 since floating point math will so rarely work with exact equality. Try > 0.98 and lowering this if you want less precise “straight” values.

it barely prints straight on 0.98 value

What you can also do is convert your dot result into degrees and set your treshold in degrees like this:

if math.deg(math.acos(lookvector:Dot(targetVector))) <= 20 then
     print("Straight")
end

That might help with setting a good threshold set up.

my car does have a angle steering system, but could you give me a simple one?

if you are trying to determine which way to turn using the RightVector may prove more useful, this way you should be able to say

if dot < 0 then
    print("turn left")
elseif dot > 0 then
    print("turn right")
else
    print("straight! (vs right vector)")
end

I’ve included a graphic, the green tank is not actually facing the direction we are just using that vector to do our math. The top part is using LookVector where the bottom is easier, using RightVector

gonna try this out.
response when im done

edit: yo thanks!