Player Ball System

Hey, I am trying to make a player ball system, in which the player controls the ball by the WASD keys. However when I do such, the ball becomes broken and is messed up in the direction of which the player screen is facing. I have tried searching on YT for help. I liked hjow it worked with the characters move direction before I put it in the module. However, I found that moving/turning the ball left/right was slow and delayed, often resulting in a crash with a building or another object. I’m stuck on where to go from here.

-- this is my code in the module that controls the movement of the ball. 
 module.MoveForwards = function(player,ball)
	ball.BodyAngularVelocity.AngularVelocity = Vector3.new(player.Character.Humanoid.MoveDirection.z * 15,0,player.Character.Humanoid.MoveDirection.x * -15)
	ball.BodyAngularVelocity.MaxTorque = Vector3.new(60000,60000,60000)
end

module.MoveBackwards = function(player,ball)
	ball.BodyAngularVelocity.AngularVelocity = Vector3.new(player.Character.Humanoid.MoveDirection.z * -15,0,player.Character.Humanoid.MoveDirection.x * 15)
	
end
module.MoveLeft = function(ball)
	ball.BodyAngularVelocity.AngularVelocity = Vector3.new(20,0,0)
	
end
module.MoveRight = function(ball)
	ball.BodyAngularVelocity.AngularVelocity = Vector3.new(-20,0,0)

end
module.Jump = function(ball)
	ball.AssemblyLinearVelocity = ball.AssemblyLinearVelocity * Vector3.new(1, 0, 1) + Vector3.new(0, 75, 0)

end

Sorry, can you elaborate on that? It’s not clear what you mean.

Sure, what I mean is when the ball was turned left the screen was facing forwards, but when the player hit w to move forwards it moved the ball a different direction.

1 Like

Oh, right that makes sense.

You can convert a camera-space movement direction to a world-space on like this:

function cameraToWorldSpaceMovement(cameraSpace: Vector3): Vector3
    local worldSpace = camera:VectorToWorldSpace(cameraSpaceMovement)
    local horizontal = worldSpace * Vector3.new(1, 0, 1)
    if horizontal.Magnitude == 0 then --If looking straight up down
        return Vector3.zero
    else
        return horizontal.Unit --.Unit of (0, 0, 0) would be (NaN, NaN, NaN)
    end
end

You’ll also want to convert a desired movement direction to a torque axis and amount:

function movementToAxisAngles(movement: Vector3): (Vector3, number)
    --You miiight need to negate the axis, not sure.
    return movement.Unit:Cross(Vector3.yAxis), movement.Magnitude
end

You can use those functions like this:

function moveInDirection(cameraSpaceDirection: Vector3)
    local worldSpaceMovement = cameraToWorldSpaceMovement(cameraSpaceDirection)
    local axis, amount = movementToAxisAngles(worldSpaceMovement)
	ball.BodyAngularVelocity.AngularVelocity = axis * amount
end

module.MoveRight = function(ball)
    moveInDirection(Vector3.new(20, 0, 0))
end

– this is the code in the player’s character that executes the code in the module above.
local module = require(game.ServerScriptService.ModuleScript)
local player = game.Players.LocalPlayer
local ball = script.Parent.Ball

local deb = false
local debounce = false
local canjump = false

ball.Assembly.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Head") then
        canjump = true
    else
        canjump = false
    end
end)

while true do
    wait()
    if ball.Assembly.Velocity.y == 0 then
        debounce = true
    elseif ball.Assembly.Velocity.y <= -10 then
        debounce = false
    end
    if canjump == true and debounce == true and deb == true then
        deb = false
        module.Jump(ball)
    elseif canjump == true and debounce == false and deb == false then
        deb = true
    end
    if player.Character.Humanoid.MoveDirection.x >= 0.1 then
        module.MoveForwards(player,ball)
    elseif player.Character.Humanoid.MoveDirection.x <= -0.1 then
        module.MoveBackwards(player,ball)
    else
        ball.BodyAngularVelocity.AngularVelocity = Vector3.new(0,0,0)
    end
    
    if player.Character.Humanoid.MoveDirection.z >= 0.1 then
        module.MoveRight(ball)
    elseif player.Character.Humanoid.MoveDirection.z <= -0.1 then
        module.MoveLeft(ball)
    else
        ball.BodyAngularVelocity.AngularVelocity = Vector3.new(0,0,0)
    end
end

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