How do I get the direction the camera is turning?

I’m trying to find the direction the camera moves, Like left right up down

I want to have my glider rotate depending on which direction the camera moves

I tried subtracting the cameras previous lookvector with the current lookvector (x and y) but that didn’t work the way I intended


local InputService = game:GetService("UserInputService")
local GliderBool = false
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Character = workspace:WaitForChild(Player.Name)

local CurrentCamera = workspace.CurrentCamera
local RS = game:GetService("RunService")
local Tween = game:GetService("TweenService")
local HRP = Character:WaitForChild("HumanoidRootPart")
local GliderRE = game.ReplicatedStorage.Glider
local DestroyGliderRE = game.ReplicatedStorage.DestroyGlider

local GliderRestraint = Player.PlayerGui.GliderRestraint
local Cursor = GliderRestraint.Outer.Cursor
local CenterCursor = Cursor.CenterCursor


local CoolDown = true

local StartInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local StartGoal = {}

StartGoal.CameraOffset = Vector3.new(0, -10, 5)

local EndInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
local EndGoal = {}

EndGoal.CameraOffset = Vector3.new(0, 0, 0)

local StartGliderInfo = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local StartGliderGoal = {}
StartGliderGoal.TextTransparency = 0
StartGliderGoal.BackgroundTransparency = 0.5

local StartGliderReady = Tween:Create(Player.PlayerGui.GliderReady.TextLabel, StartGliderInfo, StartGliderGoal)

local EndGliderInfo = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
local EndGliderGoal = {}
EndGliderGoal.TextTransparency = 1
EndGliderGoal.BackgroundTransparency = 1

local EndGliderReady = Tween:Create(Player.PlayerGui.GliderReady.TextLabel, EndGliderInfo, EndGliderGoal)

local CoolDownImageInfo = TweenInfo.new(120, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
local CoolDownImageGoal = {}
CoolDownImageGoal.Offset = Vector2.new(0,1)


local CoolDownImageTween = Tween:Create(Player.PlayerGui.GliderReady.Frame.Frame.UIGradient, CoolDownImageInfo, CoolDownImageGoal)
InputService.InputBegan:Connect(function(key, gameProcessedEvent)
	if gameProcessedEvent then return end
	if key.KeyCode == Enum.KeyCode.Two then
		
		if CoolDown then
			CoolDown = false
			GliderBool = true
			local HRPPreviousPositionY = HRP.CFrame.Y
			local PreviousMouseDelta = InputService:GetMouseDelta()
			
			InputService.MouseBehavior = Enum.MouseBehavior.LockCenter
			
			local Glider = Character:WaitForChild("Glider")
			local Attachment = Instance.new("Attachment")
			Attachment.Parent = HRP
			
			local AlignOrientation = Instance.new("AlignOrientation")
			AlignOrientation.Parent = HRP

			AlignOrientation.Attachment0 = Character.UpperTorso.BodyBackAttachment
			AlignOrientation.Attachment1 = Glider:WaitForChild("Turn").Attachment1
			
			local BodyVelocity = Instance.new("BodyVelocity")
			BodyVelocity.Parent = HRP

			Character.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
			wait(.1)
			for i,v in pairs(Character.Humanoid:GetPlayingAnimationTracks()) do
				print(v)
				v:Stop()

			end

			local ChangeCameraOffset = Tween:Create(Character.Humanoid, StartInfo, StartGoal)
			ChangeCameraOffset:Play()
			local Render = RS.RenderStepped:Connect(function(dt)
				--GliderBool prevents this from running when another tool is using renderstepped
				if GliderBool then

					local Direction = CFrame.new(HRP.Position, Mouse.Hit.Position).LookVector
					--local MouseY = math.clamp(Mouse.Y, -180, 180)*-1/10
					--local MouseX = math.clamp(Mouse.X, -180, 180)*-1/10
					local ViewportY = CurrentCamera.ViewportSize.Y/2
					local ViewportX = CurrentCamera.ViewportSize.X/2														
					local MouseY = (Mouse.Y - ViewportY) / 10
					local MouseX = (Mouse.X - ViewportX) * -1
					
					local MouseXClamp = math.clamp(MouseX, -90, 90)
					local MouseYClamp = math.clamp(MouseY, -180, 180)
					
					local MouseDistanceFromCenter = Vector2.new(Mouse.X, Mouse.Y)
					
					local HRPCurrentPositionY = HRP.CFrame.Y
					
					local HRPDifference = HRPPreviousPositionY - HRPCurrentPositionY
					
					local CurrentMouseDelta = InputService:GetMouseDelta()
					local TurnX = 0
					local TurnY = 0
					
					
					local CameraCurrentOrientationX, CameraCurrentOrientationY, CameraCurrentOrientationZ = CurrentCamera.CFrame:ToEulerAnglesXYZ()
					
					
					local CurrentOrientation = CurrentCamera
					Cursor.Position = UDim2.new(MouseDistanceFromCenter)
					
					--HRP.CFrame = CFrame.new(HRP.Position, Vector3.new(Mouse.Hit.X, Mouse.Hit.Y, Mouse.Hit.Z)) * CFrame.Angles(math.rad(-90), 0, 0)
					local CurrentGliderYOrientation = Glider.Turn.CFrame.Y
					print(math.abs(PreviousMouseDelta.Y) - math.abs(CurrentMouseDelta.Y))
					if math.abs(PreviousMouseDelta.Y) - math.abs(CurrentMouseDelta.Y) > 0 then
						TurnX = 1
					elseif math.abs(PreviousMouseDelta.Y) - math.abs(CurrentMouseDelta.Y) < 0 then
						TurnX = -1
					else TurnX = 0
					end
					
					
					Glider.Turn.CFrame = CFrame.new(Glider.Turn.Position, Vector3.new(Mouse.Hit.X, Glider.Turn.Position.Y, Mouse.Hit.Z)) * CFrame.Angles(0,math.rad(-90), 0) * CFrame.Angles(math.rad(math.rad(TurnX*40)), 0,  math.rad(0))

					HRPPreviousPositionY = HRP.CFrame.Y
					PreviousMouseDelta = InputService:GetMouseDelta()
					
					BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
					BodyVelocity.Velocity = Direction * 40


				end

			end)

			wait(10)

			GliderBool = false
			DestroyGliderRE:FireServer()
			InputService.MouseBehavior = Enum.MouseBehavior.Default
			local ChangeCameraOffsetEnd = Tween:Create(Character.Humanoid, EndInfo, EndGoal)
			ChangeCameraOffsetEnd:Play()
			
			Character.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
			Character.Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
			BodyVelocity:Destroy()
			Render:Disconnect()
		end
		Player.PlayerGui.GliderReady.Frame.Frame.UIGradient.Offset = Vector2.new(0, 0)
		Player.PlayerGui.GliderReady.Frame.Frame.Transparency = 0.5

		CoolDownImageTween:Play()
		wait(120)
		Player.PlayerGui.GliderReady.Frame.Frame.Transparency = 1
		Player.PlayerGui.GliderReady.Frame.Frame.UIGradient.Offset = Vector2.new(0, 0)
		GliderRE:FireServer()
		CoolDown = true
		StartGliderReady:Play()
		wait(2)
		EndGliderReady:Play()
	end
end)
2 Likes

There is already a post about this which I think should solve your problem:

2 Likes

Instead of subtracting the previous LookVector from the current one, try subtracting the Y value of the orientation.

This was the original idea, The mouse is locked to center of screen so the mouse.x and mouse.y will be 0 which ever way I look.

I’ll try this and I’ll let you know

1 Like

Use GetMouseDelta method of user input service which returns delta position specifically for the locked mouse

That did work now I come across the issue of when I’m turning it rotates incorrectly I might try math.abs but I’m not 100% that will work

could you provide the code you currently have and perhaps screenshot or video of your glider

I updated my original post to have the script

https://gyazo.com/ee70cf1d20c2e837899ba21beff85796

1 Like

alright i see you are using align orientation

local AlignOrientation = Instance.new("AlignOrientation")
AlignOrientation.Parent = HRP

AlignOrientation.Attachment0 = Character.UpperTorso.BodyBackAttachment
AlignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment

-- update rotation
local Update = RunService.Heartbeat:Connect(function(dt)
	local CamLV = Camera.CFrame.LookVector
	-- since we need to rotate on XZ plane we need to eliminate Y axis
	local Rotation = (CamLV - Vector3.new(0, CamLV.Y, 0)).Unit

	AlignOrientation.CFrame = CFrame.new(Vector3.zero, Rotation)
end)


yeah i hope i used it the right way

I got the glider to turn thats a discrepancy on my part. I want it to rotate the character sideways. I apprectiate your help

ohh so do you want the character to lean on the side while turning?

try this

local AlignOrientation = Instance.new("AlignOrientation")
AlignOrientation.Parent = HRP

AlignOrientation.Attachment0 = Character.UpperTorso.BodyBackAttachment
AlignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment

-- update rotation
local Update = RunService.Heartbeat:Connect(function(dt)
	-- perhaps you need to negate camere lookvector since the camera is facing -Z axis
	local CamLV = -Camera.CFrame.LookVector
	-- since we need to rotate on XZ plane we need to eliminate Y axis
	local Rotation = (CamLV - Vector3.new(0, CamLV.Y, 0)).Unit
	local Angle = math.acos(Rotation:Dot(AlignOrientation.Atrachment0.CFrame.LookVector))

	AlignOrientation.CFrame = CFrame.new(Vector3.zero, Rotation) * CFrame.Angles(0, 0, Angle)
end)

I had tried this, it worked fine until I center locked my mouse. I found an avatar the last Airbender glider that does what I need but the math is so complex to me I need awhile to parse through what each line of math does.