Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does the ball orbiting the player go crazy when the player moves?

Asked by
tjtorin 172
5 years ago

I create a ball that is supposed to rotate the player at 90 deg per second and it works fine when the player spawns but when the player starts to move the ball starts getting farther away from the player.

Local

local RS = game:GetService("RunService")
local RepStore = game:GetService("ReplicatedStorage")
local rotatePart = RepStore:WaitForChild("RotatePart_E")

function rotatePoint(point, center, angle)
    angle = angle * (math.pi / 180) -- Convert to radians
    local rotX = math.cos(angle) * (point.x - center.x) - math.sin(angle) * (point.z - center.z) + center.x
    local rotZ = math.sin(angle) * (point.x - center.x) + math.cos(angle) * (point.z - center.z) + center.z

    return {rotX, rotZ}
end


rotatePart.OnClientEvent:Connect(function(center, ball)
    RS.RenderStepped:Connect(function(delta)
        local rotateBy = 90 * delta

        local ballPosition = {x = ball.Position.X, z = ball.Position.Z}
        local centerPosition = {x = center.Position.X, z = center.Position.Z}

        -- Update position
        local rotatedPosition = rotatePoint(
            ballPosition,
            centerPosition,
            rotateBy
        )

        ball.Position = Vector3.new(
            rotatedPosition[1],
            center.Position.Y,
            rotatedPosition[2]
        )
    end)
end)

Server

local RepStor = game:GetService("ReplicatedStorage")
local rotatePart = Instance.new("RemoteEvent")
rotatePart.Name = "RotatePart_E"
rotatePart.Parent = RepStor

game.Players.PlayerAdded:Connect(function(plr)
    -- local center = workspace:WaitForChild("CenterPart")
    local char = workspace:WaitForChild(plr.Name)
    local center = char:FindFirstChild("HumanoidRootPart")

    local ball = Instance.new("Part")
    ball.Parent = center
    ball.Shape = "Ball"
    ball.Anchored = true
    ball.CanCollide = false
    ball.BrickColor = BrickColor.new("Really red")
    ball.Size = Vector3.new(1, 1, 1)
    ball.Position = Vector3.new(
        center.Position.X + 3, 
        center.Position.Y, 
        center.Position.Z + 3
    )

    rotatePart:FireClient(plr, center, ball)
end)

Answer this question