Hello,
How would I be able to make a player controlled ball? I saw someone else was able to make it in 11 minutes from scratch and he used sines and velocity and whatnot.
Here’s my localscript, it’s just a localscript in starterplayerscripts. There are no other scripts and the ball is created in the script.
repeat wait() until game.Players.LocalPlayer.Character
repeat wait() until game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")
local char = game.Players.LocalPlayer.Character
for i,v in pairs(char:GetChildren()) do
if v:IsA("BasePart") then
v.Transparency = 1
if v.Name~="HumanoidRootPart" and v.Name~="Head" and v.Name~="Torso" then
print(v.Name)
v:Destroy()
end
end
if v:IsA("Accessory") or v:IsA("Hat") then
v:Destroy()
end
end
local hum = char.HumanoidRootPart
local ball = Instance.new("Part",char)
ball.Size=Vector3.new(5,5,5)
ball.Position=Vector3.new(0,5,0)
hum.CFrame = ball.CFrame
local weld = Instance.new("WeldConstraint",hum)
weld.Part0=hum
weld.Part1=ball
local weld = Instance.new("WeldConstraint",hum)
weld.Part0=hum
weld.Part1=char.Head
ball.Material = Enum.Material.Granite
ball.Shape = Enum.PartType.Ball
local mou = game.Players.LocalPlayer:GetMouse()
local velo = Instance.new("BodyVelocity",ball)
velo.MaxForce=Vector3.new(10^10^10,10^10^10,10^10^10)
mou.KeyDown:Connect(function(k)
if k.lower=="w" then
velo.Velocity=ball.CFrame.lookVector*16
end
end)
When I do click play however, the ball just floats up and I can only rotate the ball with the WASD keys.
- Br, iSyriux
Hello,
Update: I changed the script a bit and now I use angularvelocity but the ball still rotates in a very weird way.
repeat wait() until game.Players.LocalPlayer.Character
repeat wait() until game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")
local char = game.Players.LocalPlayer.Character
for i,v in pairs(char:GetChildren()) do
if v:IsA("BasePart") then
v.Transparency = 1
if v.Name~="HumanoidRootPart" and v.Name~="Head" and v.Name~="Torso" then
print(v.Name)
v:Destroy()
end
end
if v:IsA("Accessory") or v:IsA("Hat") then
v:Destroy()
end
end
local hum = char.HumanoidRootPart
local ball = Instance.new("Part",char)
ball.Size=Vector3.new(5,5,5)
ball.Position=Vector3.new(0,5,0)
hum.CFrame = ball.CFrame
local weld = Instance.new("WeldConstraint",hum)
weld.Part0=hum
weld.Part1=ball
local weld = Instance.new("WeldConstraint",hum)
weld.Part0=hum
weld.Part1=char.Head
ball.Material = Enum.Material.Granite
ball.Shape = Enum.PartType.Ball
local mou = game.Players.LocalPlayer:GetMouse()
local velo = Instance.new("BodyAngularVelocity",ball)
velo.MaxTorque=Vector3.new(10^10^10,10^10^10,10^10^10)
mou.KeyDown:Connect(function(k)
if string.lower(k)=="w" then
velo.AngularVelocity=ball.CFrame.lookVector*char.Humanoid.WalkSpeed
end
if string.lower(k)=="d" then
velo.AngularVelocity=ball.CFrame.rightVector*char.Humanoid.WalkSpeed
end
if string.lower(k)=="a" then
velo.AngularVelocity=ball.CFrame.rightVector*-char.Humanoid.WalkSpeed
end
if string.lower(k)=="s" then
velo.AngularVelocity=ball.CFrame.lookVector*-char.Humanoid.WalkSpeed
end
end)
mou.KeyUp:Connect(function(k)
if string.lower(k)=="w" then
velo.AngularVelocity=Vector3.new(0,0,0)
end
if string.lower(k)=="d" then
velo.AngularVelocity=Vector3.new(0,0,0)
end
if string.lower(k)=="a" then
velo.AngularVelocity=Vector3.new(0,0,0)
end
if string.lower(k)=="s" then
velo.AngularVelocity=Vector3.new(0,0,0)
end
end)
Sorry I was in class, it do be like that sometimes. Basically what you want to do is not rely on the humanoid system at all, rather have the player be stationary and have it update a data model. Where that data models speed correlates to the body force
Synapse
local speed = 10
local dir = Vector3.new()
game:GetService("RunService").RenderStepped:Connect(function(dt)
object.BodyForce = ((dir * speed) + Vector3.new(0,object:GetMass() *workspace.Gravity, 0)) * dt
end)
EDIT: I suppose you can translate direction to rotation by for example movement on x is correlated with the z axis and z movement is correlated to x rotation. It is really simple to understand why if you understand what rotation in roblox truly is, the rotation of a object upon an axis. Almost like a pole dancer.
Hello,
I’m sorry but can you reiterate? There’s still so many unanswered questions. If the humanoid model was locked in place, how would you be able to control the movement of the ball using WASD keys?
local UserInputService = game:GetService'UserInputService'
local RunService = game:GetService'RunService'
local Players = game:GetService'Players'
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character
local RootPart = Character:WaitForChild'HumanoidRootPart'
local Humanoid = Character:WaitForChild'Humanoid'
local Camera = workspace.CurrentCamera
local Ball = Instance.new("Part", RootPart)
Ball.Size = Vector3.new(10, 10, 10)
Ball.Position = Vector3.new(0, 5, 0)
Ball.Material = Enum.Material.Granite
Ball.Shape = Enum.PartType.Ball
Ball.Transparency = 0.5
RootPart.CFrame = Ball.CFrame
local Weld = Instance.new("WeldConstraint", RootPart)
Weld.Part0 = Ball
Weld.Part1 = RootPart
Humanoid.PlatformStand = true
local BodyForce = Instance.new("BodyForce", Ball)
local function IsWASD(Key)
return Key == Enum.KeyCode.W or Key == Enum.KeyCode.A or Key == Enum.KeyCode.S or Key == Enum.KeyCode.D
end
local function IsKeyDown(Key)
return UserInputService:IsKeyDown(Key)
end
local Speed = 100
local Connection
UserInputService.InputBegan:Connect(function(Input, GameProcessed)
if GameProcessed then return end
local Key = Input.KeyCode
-- if key is W/A/S/D and force updater isn't already connected then connect it
if not IsWASD(Key) or Connection then return end
print'Connected'
Connection = RunService.RenderStepped:Connect(function()
local Force = Vector3.new(0, 0, 0)
-- Apply force for corresponding key
if IsKeyDown'W' then
Force += (Camera.CFrame.LookVector * Vector3.new(1, 0, 1)).Unit * Ball:GetMass() * Speed
end
if IsKeyDown'A' then
Force -= (Camera.CFrame.RightVector * Vector3.new(1, 0, 1)).Unit * Ball:GetMass() * Speed
end
if IsKeyDown'S' then
Force -= (Camera.CFrame.LookVector * Vector3.new(1, 0, 1)).Unit * Ball:GetMass() * Speed
end
if IsKeyDown'D' then
Force += (Camera.CFrame.RightVector * Vector3.new(1, 0, 1)).Unit * Ball:GetMass() * Speed
end
BodyForce.Force = Force
end)
end)
UserInputService.InputEnded:Connect(function(Input, GameProcessed)
if GameProcessed then return end
local Key = Input.KeyCode
-- If movement keys aren't pressed and InputEnded fired on a WASD key then disconnect force update connection
if not IsWASD(Key) or IsKeyDown'W' or IsKeyDown'A' or IsKeyDown'S' or IsKeyDown'D' then return end
if Connection then
print'Disconnected'
Connection:Disconnect()
Connection = nil
BodyForce.Force = Vector3.new(0, 0, 0)
end
end)
Hello,
I don’t just want code. I want an explanation on how it works, how I can make it, and how to implement it. That code you gave me was from EgoMoose. Just copying and pasting code here is against the rules on devforum.
I wrote this code myself but ok. When a key is pressed you use the camera’s lookvector/rightvector and convert it into a 2d plane to know which way the ball should move. For example when W is pressed, you take the lookvector and then remove the Y component by multiplying the lookvector by Vector3.new(1, 0, 1) so there’s no unnecessary Y forces acting on the ball when you tilt your camera. Then you convert that vector into a Unit vector that’s on the X and Z plane to get the direction that the ball should be moving relative to the lookvector of the camera. Then you multiply the direction vector by the ball’s mass so the speed of the ball stays consistent no matter the mass of the ball. Then you multiply by Speed which is just a speed multiplier
Note: This reply got some info wrong, don’t quote it
Hi it really is very lucked base on whether or not people see your post, instead of getting frustrated, all you can really do is wait, even if it takes a while.
Example: Random video on YT gets 15 million views but another video that’s just as funny gets 2000 views. On the devforum, post visibility works differently and not getting a reply for a while is normal.
Not everyone is going to have an answer to your question. Especially one that seems to be extremely specific and perhaps not something that a lot of people have experience in making. So next time you don’t get an answer in the first hour of posting your question, don’t go blasting the people on the devforum who take the time out of their day to answer people’s questions such as your own.
I also think the answer provided by @hell_ish looks to be pretty good and I would recommend you to read it if you haven’t already.
Hello, I took the time to read your post since I wanna help and I just wanted to let you know that Mouse.KeyDown is deprecated and that you should probably replace it with UserInputService.InputBegan. The same is true for key up. I’ll be looking into a solution for you. However, I don’t think I can due to not many people, including me, have done something like this yet, and that the solution provided by @hell_ish could be tested out.
For example if the player press’s w we need to translate that into a direction. That being, relative to the direction of the camera. So if we are holding w we can describe it as going forward relative to the camera. We then correct this by finding the delta difference of the vectors and then, like i said rotating it around the axis, almost like a pole dancer, locked to a specific pole or axis in a sense
if bodyvelocity = 0.00 the humanoid cant recognize it as being stationary
the reason why the model is going up is because the humanoid is applying force but the bodyvelocity = 0
this might help you stop it: