#Character controller code not working

1 messages · Page 1 of 1 (latest)

pliant pilot
#

so im making a 2d fighting game like mortal kombat etc i have the camera working and im trying to get movement working for the camera i need two characters so i added a dummy character and im trying to get them to always face eachother no matter what and stay locked on a z axis so they dont go off track but its not working the code for staying on the z axis only works on the player and there facing the wrong direction can someone help me fix this so i can get to the fun stuff here is the code

signal stoneBOT
#

studio** You are now Level 1! **studio

pliant pilot
#

-- ReplicatedStorage.Modules.CharacterController

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

local CharacterController = {}

function CharacterController:Init(character, options)
local isPlayer = options and options.isPlayer or false
local inputEnabled = isPlayer

local root = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
humanoid.AutoRotate = false

local speed = options.speed or 18
local jumpForce = options.jumpForce or 60

local keysHeld = { A = false, D = false, W = false }

if inputEnabled then
    local UIS = game:GetService("UserInputService")

    UIS.InputBegan:Connect(function(input, processed)
        if processed then return end
        local key = input.KeyCode
        if key == Enum.KeyCode.A then keysHeld.A = true end
        if key == Enum.KeyCode.D then keysHeld.D = true end
        if key == Enum.KeyCode.W then keysHeld.W = true end
    end)

    UIS.InputEnded:Connect(function(input)
        local key = input.KeyCode
        if key == Enum.KeyCode.A then keysHeld.A = false end
        if key == Enum.KeyCode.D then keysHeld.D = false end
        if key == Enum.KeyCode.W then keysHeld.W = false end
    end)
end

RunService.RenderStepped:Connect(function()
    -- Movement for player
    if inputEnabled then
        local direction = 0
        if keysHeld.A and not keysHeld.D then direction = -1 end
        if keysHeld.D and not keysHeld.A then direction = 1 end
        root.Velocity = Vector3.new(direction * speed, root.Velocity.Y, 0)

        if keysHeld.W then
            if humanoid.FloorMaterial ~= Enum.Material.Air then
                root.Velocity = Vector3.new(root.Velocity.X, jumpForce, 0)
            end
            keysHeld.W = false -- Prevent holding jump
        end
    end

    -- Lock Z position and warn if off-axis
    local pos = root.Position
    if math.abs(pos.Z) > 0.01 then
        warn(character.Name .. " drifted off Z-axis! Snapping to 0. Z = " .. tostring(pos.Z))
    end
    root.CFrame = CFrame.new(pos.X, pos.Y, 0)
    local v = root.Velocity
    root.Velocity = Vector3.new(v.X, v.Y, 0)

    -- Find opponent: include both Players and Models (like DummyFighter)
    local opponent = nil
    for _, model in pairs(workspace:GetChildren()) do
        if model:IsA("Model") and model ~= character and model:FindFirstChild("HumanoidRootPart") then
            opponent = model:FindFirstChild("HumanoidRootPart")
            break
        end
    end

    -- Face opponent left/right
    if opponent then
        local facingLeft = opponent.Position.X < root.Position.X
        local faceCFrame = CFrame.new(root.Position.X, root.Position.Y, 0)
        if facingLeft then
            faceCFrame *= CFrame.Angles(0, math.rad(180), 0)
        end
        character:SetPrimaryPartCFrame(faceCFrame)

        print(character.Name .. " facing " .. (facingLeft and "left" or "right") .. " toward " .. opponent.Parent.Name)
    else
        print(character.Name .. " has no opponent to face.")
    end
end)

end

return CharacterController