I am currently working on a gun and want to change the character’s Orientation depending on where I look.
I’ve encountered an issue though, as everytime Im changing the HumanoidRootParts Orientation, the player falls and overall behaves weirdly (watch video).
As Im very unexperienced with CFrames.Angles and that stuff, I have problems finding the solution by my own.
Thank you in advance for any help!
Here is the script and the video:
gun.Activated:Connect(function()
isPressed = true
if PlayerStats["LastShot"] and tick() - PlayerStats["LastShot"] > waitingTime then -- not on Cooldown
while isPressed do
-- Correcting Humanoid:
local humP = character:FindFirstChild("HumanoidRootPart")
if humP then
humP.CFrame = CFrame.new(humP.Position, Vector3.new(camera.CFrame.LookVector.X, 0, camera.CFrame.LookVector.Z).Unit)
end
end
end
end)
You’re making the character face towards the direction, but the problem is you didn’t add the HumanoidRootPart’s position to it, so the character pretty much looks at (0, 0, 0)
to fix this, replace CFrame.new with CFrame.lookAlong, which was newly added and makes a CFrame facing along a direction
it’s equivalent to doing CFrame.new(pos, pos + direction), but it’s shorter
Yeah, just use CFrame.lookAlong, which will make the CFrame have the LookVector that you specify.
Code:
gun.Activated:Connect(function()
isPressed = true
if PlayerStats["LastShot"] and tick() - PlayerStats["LastShot"] > waitingTime then -- not on Cooldown
while isPressed do
-- Correcting Humanoid:
local humP = character:FindFirstChild("HumanoidRootPart")
if humP then
local lookVector = camera.CFrame.LookVector
local modifiedVector = Vector3.new(lookVector.X, 0, lookVector.Z)
humP.CFrame = CFrame.lookAlong(humP.Position, modifiedVector)
end
task.wait()
end
end
end)
I added a task.wait() so your script doesn’t time out.