I’m trying to prototype a climbing game and when the player isn’t using their hands i want them to follow the camera at the bottom of the screen. However, when I tried doing this my solution isn’t properly holding the position when rotating and when looking straight up or down.
Heres a recording of what I mean:
As you can see, the hands positioning is okay when looking straight, but as soon as the camera begins to look up or down they drift closer together.
Heres a copy of my code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local function AdjustHandPosition(hand : Instance)
local cameraCFrame = camera.CFrame
local cameraUnitLookVector = cameraCFrame.LookVector.Unit
local leftHandOffset = CFrame.new(2,-1,-2)
local rightHandOffset = CFrame.new(2,-1,2)
leftHandOffset = Vector3.new(
(leftHandOffset.X * cameraUnitLookVector.X) - (leftHandOffset.Z * cameraUnitLookVector.Z),
(leftHandOffset.X * cameraUnitLookVector.Y) + (leftHandOffset.Y * (math.abs(cameraUnitLookVector.X) + math.abs(cameraUnitLookVector.Z))),
(leftHandOffset.X * cameraUnitLookVector.Z) + (leftHandOffset.Z * cameraUnitLookVector.X)
)
rightHandOffset = Vector3.new(
(rightHandOffset.X * cameraUnitLookVector.X) - (rightHandOffset.Z * cameraUnitLookVector.Z),
(rightHandOffset.X * cameraUnitLookVector.Y) + (rightHandOffset.Y * (math.abs(cameraUnitLookVector.X) + math.abs(cameraUnitLookVector.Z))),
(rightHandOffset.X * cameraUnitLookVector.Z) + (rightHandOffset.Z * cameraUnitLookVector.X)
)
print(cameraCFrame.LookVector)
print(rightHandOffset)
if hand.Name == "HandLeft" then
local goal = cameraCFrame + (leftHandOffset)
local tween = TweenService:Create(hand, TweenInfo.new(0.05, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {CFrame = goal})
tween:Play()
elseif hand.Name == "HandRight" then
local goal = cameraCFrame + (rightHandOffset)
local tween = TweenService:Create(hand, TweenInfo.new(0.05, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {CFrame = goal})
tween:Play()
end
end
local function InitializeClimbing()
local handLeft = ReplicatedStorage:WaitForChild("HandTemplate"):Clone()
local handRight = ReplicatedStorage:WaitForChild("HandTemplate"):Clone()
handLeft.Name = "HandLeft"
handRight.Name = "HandRight"
handLeft.Parent = player.Character
handRight.Parent = player.Character
RunService.Stepped:Connect(function()
AdjustHandPosition(handLeft)
AdjustHandPosition(handRight)
end)
end
player.CharacterAdded:Connect(function()
InitializeClimbing()
end)
I couldn’t find any solutions to this problem when I looked around on the forum, so I’d really appreciate if someone could point me in the right direction with all of this. I have a feeling that my solution to this problem is pretty naive, but I don’t have that much experience handing problems like these. Also, forgive me if my code is sloppy, I wrote it in 2 hours late last night and this is just for prototyping.