Player movements gets glitched / bugged (custom camera)

Apparently sometimes the player movment gets bugged, It occurs randomly i.e. it sometimes works fine and sometimes it doesn’t

Here’s a video of both bugged and non bugged

https://gyazo.com/8685f68c99d0f3fd2a72e44f231aea14
(How it should work)

https://gyazo.com/420f1f1a4653a0fa26c4f909fe18c2f5
(How it does sometimes)

Only scripts affecting the player movement I can think of are the custom top down camera script and the “look at mouse” script

Top down camera:

--Made by Beastcraft_Gaming

--Variables--
local TweenService = game:GetService("TweenService");

local player: Player = game.Players.LocalPlayer;
local character: Model = player.Character or player.CharacterAdded:Wait();
local humanoid = character:WaitForChild("Humanoid");
local humPart = character:WaitForChild("HumanoidRootPart");
local head = character:WaitForChild("Head");

local mouse = player:GetMouse();
local mousePosition

local camera = workspace.CurrentCamera;
local camHeight = script:GetAttribute("Cam_Height");

local movementOffset

local tweenTime

local maxDist 
--Main

camera.CameraType = Enum.CameraType.Scriptable;
--camera.CameraSubject = humPart;

humanoid.AutoRotate = false

game:GetService("RunService").RenderStepped:Connect(function()
    
    camHeight = script:GetAttribute("Cam_Height");
    maxDist = script:GetAttribute("Max_Dist");
    movementOffset = script:GetAttribute("Mov_Offset");
    tweenTime = script:GetAttribute("Tween_Time");
    
    local mouseCFrame = mouse.Hit;
    
    --distance between character and mouse and it's unit vector
    local dist = (mouseCFrame.Position - humPart.Position).Magnitude
    local unitVector = (mouseCFrame.Position - humPart.Position).Unit;
    
    --clamping the vector
    local offsetVector = unitVector * math.min(dist, maxDist) * movementOffset;
    
    --Applying the Offset
    local newCFrame = CFrame.new(humPart.Position + Vector3.new(offsetVector.X,camHeight, offsetVector.Z)) * CFrame.Angles(math.rad(-90),0,0);
    
    --Tweening
    local goal = {["CFrame"] = newCFrame};
    
    local tweenInfo = TweenInfo.new(tweenTime);
    
    local tween = TweenService:Create(camera, tweenInfo, goal);
    tween:Play();
    
    
end)

Look At Mouse script:

--Made by Beastcraft_Gaming

--Variables--

--Services
local replicatedStorage = game:GetService("ReplicatedStorage");
local RunService = game:GetService("RunService");

--Events
local LookAtMouseEvent = replicatedStorage:WaitForChild("Events"):WaitForChild("LookAtMouse");

--Player Variables
local player = game.Players.LocalPlayer;
local character = player.Character or player.CharacterAdded:Wait();

local humPart = character:WaitForChild("HumanoidRootPart");
local torso = character:WaitForChild("Torso");
local mouse = player:GetMouse();

local range = 10;

RunService.RenderStepped:Connect(function()
    
    tweenTime = script:GetAttribute("Tween_Time");
    
    local mousePosition = mouse.Hit.Position;
    
    local dist = (Vector3.new(mousePosition.X,0, mousePosition.Z) - Vector3.new(humPart.CFrame.Position.X, 0, humPart.CFrame.Position.Z)).Magnitude;
    
    if dist > range then 
        
        humPart.CFrame = CFrame.lookAt(humPart.Position, Vector3.new(mousePosition.X, 0, mousePosition.Z));
        
    end
end)

Note: I have tried solutions like making humanoid.AutoRotate = false but no success

3 Likes

Hey , i have tried making a similar type of custom camera before and it worked fine for me.

Try chaning the mousePosition variable to

local mousePosition = mouse.Hit.p
1 Like

The camera works just fine but the movement sometimes gets glitched

Since it occurs randomly is it a problem in my scripts or a problem in roblox?

1 Like

from what I read it seems you are setting the HRP cframe only when range is over 10 this maybe what is causing it sometimes because the humanoid is trying to move to new position but you are constantly resetting the cframe for rotation you might try using a AlignOrientation setup instead of setting the HRP cframe over what the humanoid is trying to do on movement to a position

also why are you referencing to Torso if I may ask?

Yes because if it’s too close the character starts rotating on y axis too

Oh It’s actually from old code forgot to remove it

Okay, thanks for suggestion I’ll look into it and tell the result

1 Like

Hello,

I tried using AlignOrientation and it works perfectly but still having the movement issue.

here’s the updated script:

--Made by Beastcraft_Gaming

--Variables--

--Services
local replicatedStorage = game:GetService("ReplicatedStorage");
local RunService = game:GetService("RunService");

--Events
local LookAtMouseEvent = replicatedStorage:WaitForChild("Events"):WaitForChild("LookAtMouse");

--Player Variables
local player = game.Players.LocalPlayer;
local character = player.Character or player.CharacterAdded:Wait();

local humPart = character:WaitForChild("HumanoidRootPart");
local mouse = player:GetMouse();

local range = 10;

local attachment: Attachment = Instance.new("Attachment", humPart);

local alignOrientation = Instance.new("AlignOrientation", humPart);
alignOrientation.Attachment0 = attachment
alignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment;
alignOrientation.RigidityEnabled = true;
alignOrientation.PrimaryAxisOnly = true;

RunService.RenderStepped:Connect(function()
    
    
    local mousePosition = mouse.Hit.Position;
    
    local dist = (Vector3.new(mousePosition.X,0, mousePosition.Z) - Vector3.new(humPart.CFrame.Position.X, 0, humPart.CFrame.Position.Z)).Magnitude;
    
    if dist > range then 
        
        --humPart.CFrame = CFrame.lookAt(humPart.Position, Vector3.new(mousePosition.X, 0, mousePosition.Z));
        local finalCFrame = CFrame.lookAt(humPart.Position, mousePosition);
        
        alignOrientation.CFrame = finalCFrame;
    end
end)

how are you actually moving the humanoid? is it using the default movement on click or do you have another script setting position for it like WalkToPoint or WalkToPart?

also depending on the full use case here you could just setup something like this letting the humanoid still control rotation with autorotate still on

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild('Humanoid')
local Down  -- used as debouce and for continual set

Mouse.Button1Down:Connect(function()
	if Down then return end
	Down = true
	while Down do
		Humanoid.WalkToPoint = Mouse.Hit.p
		wait()
	end
end)
Mouse.Button1Up:Connect(function()
	Down = false
end)

It’s actually default movement,

Actually if I don’t find any solution to this I am planning to switch to custom movement

Also your humanoid moving looks like you have a BodyPosition in your humrp or torso that holds your character in one place.I don’t think it’s so because your camera script have some errors.

so you are using the click to move

I don’t want character to move using mouseclick, character moves using keyboard and faces towards the mouse no clicking is included

No, no bodymover or anythings is there it’s roblox movement the only thing I am doing is making character face the mouse

Check if your character have something in torso or humrp like body position.

If that so i guess the problem is changing cframe of humrp or torso in every heartbeat

nope nothing except an alignorientation used to rotate the character

ok knowing what you are trying to do exactly now seems you will need to write a custom movement for the player by facing direction other than that only thing limiting the rotation is your 10 stud limit that i can see

If you mean by changing runService.RenderStepped:Connect() to runService.HeartBeat:Connect() then also still having the issue

Actually the limit is no longer needed since alignOrientation has “PrimaryAxisonly” by which it only rotates on one axis

1 Like

I haven’t used align orientation before. Why do you you need it there??