How can I know which direction to turn to?

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to make a sliding feature which instead of instantly turning the velocity towards the desired angle turns you slowly instead.

  2. What is the issue?
    The issue I am facing is that I don’t know how I can use the measured angle (using dot product) to turn towards the desired direction based on a value, since the dot product is always positive/negative no matter if you’re meant to look left or right.

  3. What solutions have you tried so far?
    I tried looking through the devforum and asking ChatGPT for help but it didn’t help much.

Here’s the script I used to test this outside of the character:

local turn_speed = math.pi/4

local prev_dot = Vector3.zero


while true do
	local dt = task.wait(0.1)
	local dot = script.Parent.Observer.CFrame.LookVector:Dot(script.Parent.Target.CFrame.LookVector)
	if dot > -1 then
		local start = script.Parent.Observer.CFrame
		script.Parent.Observer.CFrame = start*CFrame.Angles(0,math.clamp(math.acos(dot),0,turn_speed*dt),0)
		if dot ~= prev_dot then
			prev_dot = dot
			print(math.sin(dot))
		end
	end
end
1 Like

Okay, after a bit of messing around I found a solution!

local turn_speed = math.pi/4

local pos = Vector3.new(33, 2.505, -104)

while true do
	local dt = task.wait(0.1)

	local ob_dir = (script.Parent.Observer.CFrame.LookVector*Vector3.new(1,0,1)).Unit
	local tr_dir = (script.Parent.Target.CFrame.LookVector*Vector3.new(1,0,1)).Unit

	local dot = ob_dir:Dot(tr_dir)

	if dot > -1 and dot < 1 then
		local cross = ob_dir:Cross(tr_dir)
		local dir = cross:Dot(Vector3.new(0,1,0))
		if dir ~= 0 then
			dir = dir/math.abs(dir)
		end

		script.Parent.Observer.Orientation += Vector3.new(0,math.deg(dir*math.clamp(math.acos(dot),0,turn_speed*dt)),0)
		
		script.Parent.Observer.Position = pos
		script.Parent.Target.Position = pos+Vector3.new(0,1,0)
		print(dir)
		print(math.sin(dot))
	end
end
1 Like