Crouch Animation makes my Character move forward

does anyone know why I mvoe forward when i play a crouch anim (i havent opened studio in a while and forgot how to fix these problems)

Just tried some stuff and found out the issue is with my Camera script, i dont know how to fix it though if any one wants to take a look.

This camera system is basically a isometric kinda camera system where the camera is from above like a birds eye view but I believe the issue is just with how the character rotates to look at where ever the mouse is. Im not sure how to fix this at all.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")

local player = Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local camera = Workspace.CurrentCamera
local mouse = player:GetMouse()

camera.CameraType = Enum.CameraType.Custom

local isoAngle = math.rad(45)
local elevation = math.rad(30)
local distance = 100
local minDistance = 80 -- Minimum zoom distance
local maxDistance = 200 -- Maximum zoom distance
local zoomSpeed = 10 -- Speed of zoom when scrolling

camera.FieldOfView = 20

local function createRaycastParams()
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Blacklist
	params.FilterDescendantsInstances = {}
	return params
end

local function getBlacklistedParts()
	local blacklist = {}
	for _, part in pairs(Workspace:GetDescendants()) do
		if part:IsA("BasePart") and part:GetAttribute("CanInvis") then
			table.insert(blacklist, part)
		end
	end
	return blacklist
end

local function updateCamera()
	local character = player.Character
	if character and character.PrimaryPart then
		local focusPoint = character.PrimaryPart.Position

		local offset = Vector3.new(
			distance * math.cos(isoAngle) * math.cos(elevation),
			distance * math.sin(elevation),
			distance * math.sin(isoAngle) * math.cos(elevation)
		)
		local cameraPosition = focusPoint + offset

		camera.CFrame = CFrame.new(cameraPosition, focusPoint)
	end
end

local function rotateCharacterToMouse()
	local character = player.Character

	if character and character.PrimaryPart then
		local rootPart = character.PrimaryPart
		local raycastParams = createRaycastParams()
		raycastParams.FilterDescendantsInstances = getBlacklistedParts()

		local unitRay = camera:ScreenPointToRay(mouse.X, mouse.Y)
		local raycastResult = Workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, raycastParams)

		if raycastResult then
			local targetPosition = Vector3.new(
				raycastResult.Position.X,
				rootPart.Position.Y,
				raycastResult.Position.Z
			)

			local distanceToTarget = (rootPart.Position - targetPosition).Magnitude

			if distanceToTarget > 2 then
				rootPart.CFrame = CFrame.new(rootPart.Position, targetPosition)
			end
		end
	end
end

local function handleScroll(input)
	if input.UserInputType == Enum.UserInputType.MouseWheel then
		distance = math.clamp(distance - (input.Position.Z * zoomSpeed), minDistance, maxDistance)
	end
end

game:GetService("UserInputService").InputChanged:Connect(handleScroll)

RunService.RenderStepped:Connect(function()
	updateCamera()
	rotateCharacterToMouse()
end)

Doesn’t look like you are resetting the HipHeight of your character. Is it trying to push into the ground causing the forward movement?

I believe so. Since the script is getting the position of where ever the mosue is which is basically the floor, I believe its slightly lean the player forward in a way. Im not too sure how to fix it though.

Nevermind I fixed it, I made this other system that works perfectly.

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local function onCharacterAdded(character)
	local HRP = character:WaitForChild("HumanoidRootPart")

	RunService.RenderStepped:Connect(function()
		if Mouse and Mouse.Hit then
			local mousePos = Mouse.Hit.Position
			local lookAt = Vector3.new(mousePos.X, HRP.Position.Y, mousePos.Z)
			HRP.CFrame = CFrame.lookAt(HRP.Position, lookAt)
		end
	end)
end

if Player.Character then
	onCharacterAdded(Player.Character)
end

Player.CharacterAdded:Connect(onCharacterAdded)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.