Ad
Log in to vote
0

Is it currently possible to control a sphere like a player, but with the sphere's physics?

Asked by 5 months ago

I've been trying all of yesterday and today to try and do this, but I can't think of any way to make this work. Anyone know anything I don't?

1 answer

Log in to vote
0
Answered by 5 months ago
Edited 5 months ago

Hi Zombie,

I just tried it out and you can easily do this using BodyPositions. Just make different functions with WASD that control the BodyPosition's Position and add/subtract values from it. Here, I'll show you how I managed to do it with BodyPosition.

local uis = game:GetService("UserInputService"); -- User Input Service to make the sphere appear.
local players = game:GetService("Players"); -- Players Service, where all the players are
local player = players.LocalPlayer; -- The local player, as in your player.
local run = game:GetService("RunService"); -- RunService to run the loops smoothly.
local speed = 15; -- How fast the sphere goes.

local keys = { -- The keys that we will be using to make the sphere move.
    ["W"] = true;
    ["A"] = true;
    ["S"] = true;
    ["D"] = true;   
}

local key_statuses = { -- The current status of these keys, i.e. if they're being held down or not. Will be important later for the while loop.
    ["W"] = false;
    ["A"] = false;
    ["S"] = false;
    ["D"] = false;  
}

local funcs_with_keys = { -- The functions with the key. Basically modifying the BodyPosition's Position property according to each coordinate respectively. So, W is forward, S is backward, A is Left, and D is Right.
    ["W"] = function(sphere, bp)
        bp.Position = sphere.Position + Vector3.new(0, 0, -speed);
    end;

    ["A"] = function(sphere, bp)
        bp.Position = sphere.Position + Vector3.new(-speed, 0, 0);
    end;

    ["S"] = function(sphere, bp)
        bp.Position = sphere.Position + Vector3.new(0, 0, speed);
    end;

    ["D"] = function(sphere, bp)
        bp.Position = sphere.Position + Vector3.new(speed, 0, 0);
    end;
    ["X"] = function(sphere, hum, control_scr, cam, head) -- This destroys the sphere and makes the character normal.
        for i = sphere.Transparency, 1, 0.05 do
            sphere.Transparency = i;
            run.RenderStepped:Wait();
        end

        sphere:Destroy();
        hum.PlatformStand = false;
        control_scr.Disabled = false;

        cam.CameraType = Enum.CameraType.Scriptable; -- Make the CameraType Scriptable. (Allowing us to change some properties)
        cam.CFrame = cam.CFrame * CFrame.new(0, 0, 5); -- Changes the CFrame and scoots it back by 5 studs so that you can see the character back at normal.
        cam.CameraSubject = head; -- Makes the Subject the head, so that the camera can focus back on the head.
        cam.CameraType = Enum.CameraType.Custom; -- Makes the CameraType back to it's original camera type, which was custom.
    end
}


uis.InputBegan:Connect(function(obj, gp) -- Anonymous function connected with InputBegan event.

    local cam = workspace.Camera;
    local char = player.Character or player.CharacterAdded:Wait(); -- The player's character
    local root = char:WaitForChild("HumanoidRootPart"); -- The HumanoidRootPart, basically the part that is at the center of the character to help for better control.
    local hum = char:WaitForChild("Humanoid"); -- The Humanoid of the Character.
    local control_scr = player:WaitForChild("PlayerScripts"):WaitForChild("ControlScript"); -- The script that allows you to control your character. Need to disable it so Character's controls don't get in the way.   
    local head = char:WaitForChild("Head");

    if obj.KeyCode == Enum.KeyCode.Q and not gp and not workspace:FindFirstChild(player.Name..player.UserId) and hum.Health > 0 then -- Checks if 'Q' was pressed, because that's what makes the sphere appear.


        local sphere = Instance.new("Part"); -- The Sphere

        local bp = Instance.new("BodyPosition"); -- The BodyPosition that you will use for the Sphere.

        hum.Died:Connect(function() -- This destroys the sphere and makes the character back to normal settings since the character's dead.
            funcs_with_keys["X"](sphere, hum, control_scr, cam, head);
        end)

        sphere.Name = player.Name..player.UserId; -- The Sphere's name. Made it specific to you by using your name and your user id. Will be important for finding it later in the other functions.
        bp.MaxForce = Vector3.new(math.huge, math.huge, math.huge); -- Makes the BodyPosition's MaxForce as high as possible.
        bp.Position = sphere.Position;   -- Sets the BodyPosition's Position to the sphere.
        bp.Parent = sphere;  -- Sets the parent of the BodyPosition to the sphere;

        hum.PlatformStand = true; -- Makes the PlatformStand true, making the character unable to move and basically makes your character a straight dead corpse.
        control_scr.Disabled = true; -- Disables the control script so that the player can't control their character.

        -- Property Modifications for the Sphere (Personal Preference) --       

        sphere.Shape = "Ball"; 
        sphere.Size = Vector3.new(15, 15, 15);
        sphere.BrickColor = BrickColor:random();
        sphere.Transparency = .5;
        sphere.TopSurface = Enum.SurfaceType.Smooth;
        sphere.BottomSurface = Enum.SurfaceType.Smooth;
        sphere.Material = Enum.Material.Neon;

        -- Property Modifications for the Sphere (Personal Preference) --           

        -- You can delete this but, you need to set the sphere's position or cframe to something. Just, this is optional if you want your character to be inside of the Sphere then this will be helpful --

        sphere.CFrame = root.CFrame; -- Set the Sphere's CFrame to the Character's HumanoidRoot so that it can appear where the player's Character is.

        local weld = Instance.new("Weld"); -- Makes the weld, this will be used to connect the Character to the Sphere.
        weld.Part0 = sphere; -- Sets Part0 of the Weld to Sphere, since you're connecting the Character to the Sphere to give the Sphere control.
        weld.Part1 = root; -- Makes the Part1 of the Weld the HumanoidRootPart, since that's what you're connecting to the sphere to control the character around. 
        weld.Parent = sphere; -- Parents the weld

        -- You can delete this but, you need to set the sphere's position or cframe to something. Just, this is optional if you want your character to be inside of the Sphere then this will be helpful --
        sphere.Parent = workspace; -- Sets the sphere's parent to workspace so it can appear in the workspace.


        -- EDIT: I did this so it's easier to test and so that the camera can follow the sphere --
        cam.CameraType = Enum.CameraType.Scriptable; -- Make the CameraType Scriptable. (Allowing us to change some properties)
        cam.CFrame = cam.CFrame * CFrame.new(0, 0, 15); -- Changes the CFrame and scoots it back by 15 studs so that you can see the whole sphere.
        cam.CameraSubject = sphere; -- Makes the Subject the shere, sothat the camera can focus on where the sphere goes.
        cam.CameraType = Enum.CameraType.Custom; -- Makes the CameraType back to it's original camera type, which was custom.
        -- EDIT: I did this so it's easier to test and so that the camera can follow the sphere --
    end

    -- Setting controls for the Sphere --
    if keys[obj.KeyCode.Name] and not gp then -- This checks if the key that was pressed is in the table of keys, basically to check if it's one of the keys that will be used to control the sphere (W, A, S, D)
        local sphere = workspace:FindFirstChild(player.Name..player.UserId); -- Tries to find the Sphere.

        if sphere then -- Checks if the sphere exists.


            key_statuses[obj.KeyCode.Name] = true; -- Sets the key's status to true, since it was pressed.

            local bp = sphere:WaitForChild("BodyPosition"); -- Finds the BodyPosition.

            while key_statuses[obj.KeyCode.Name] and run.RenderStepped:Wait() do -- Checks if the key's status was true. So, that if you hold the key down, it will keep moving the sphere. Also, it waits using RenderStepped, which is 1/60th of a second, smooth frame rate.
                funcs_with_keys[obj.KeyCode.Name](sphere, bp) -- Calls the function for the key that was pressed. The functions are in the table above called funcs_with_keys. Also, passes sphere and bp through the parameters so that those functions can use them to move the sphere respectively according to their position.
            end
        end
    end

    if obj.KeyCode == Enum.KeyCode.X and not gp then -- CHecks if you pressed X
        local sphere = workspace:FindFirstChild(player.Name..player.UserId); -- Tries to find the sphere

        if sphere then -- Checks if there's a sphere


            funcs_with_keys["X"](sphere, hum, control_scr, cam, head); -- Deletes the sphere and makes the character back to normal.
        end
    end
    -- Setting controls for the Sphere --
end)

uis.InputEnded:Connect(function(obj, gp) -- Anonymous function key ends, so that it can turn the status back to normal, to stop the loop of moving the sphere.
    if keys[obj.KeyCode.Name] and not gp then -- Checks if the key that was ended is one of the keys that control the sphere (W, A, S, D)
        key_statuses[obj.KeyCode.Name] = false; -- Changes the status of the respective key.
    end
end)

This script assumes you want the character to be inside of the sphere, but you can change that with a few lines if you want and I outlined those lines in the script and gave you instructions for them.

Well, I hope I helped and have a wonderful day/night.

EDIT: I made it so that the camera is fixed now. It will follow the sphere now and is far away enough for you to see the sphere.

EDIT #2: I added a destruction of the sphere so that if the character dies or if the character presses 'X', the sphere gets destroyed smoothly and the character goes back to normal. Also, I published the game and you can take a copy here or just play it there. I also made it so that if you already have a sphere, you can't make another one and if you're dead you can't make a sphere.

Thanks,

Best regards,

~~ KingLoneCat

Ad

Answer this question