I’m trying to figure out how to fire a ball forward when clicked. It’s not going to a set position, I just want it to fire forward when clicked. What I have so far but doesn’t move right now -
local part = script.Parent
local clickDetector = script.Parent:FindFirstChild("ClickDetector")
local function onClick()
part:MoveTo(part.Position + Vector3.new(30, 0, 0))
end
clickDetector.MouseClick:Connect(onClick)
local part = script.Parent
local clickDetector = script.Parent:FindFirstChild("ClickDetector")
local function onClick()
part.CFrame = part.CFrame * CFrame.new(30, 0, 0)
end
clickDetector.MouseClick:Connect(onClick)
This does work but the ball just moves from one set position to another. I just want it to fire forward at a certain speed so it bounces around randomly.
Then you might want to use TweenService so it slides smoothly like this:
local part = script.Parent
local clickDetector = script.Parent:FindFirstChild("ClickDetector")
local tweeninfo = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
local function onClick()
local tween = game:GetService("TweenService"):Create(part,tweeninfo,{["Position"]=part.Position+Vector3.new(0,0,30)})
tween:Play()
end
clickDetector.MouseClick:Connect(onClick)
To make the ball go forward smoothly you can use BodyAngularVelocity.
What I did is: Create a Ball with 8, 8, 8 of size.and insert a ClickDetector and a Script with the following code:
local Ball = script.Parent
local ClickDetector = Ball.ClickDetector
local Debris = game:GetService("Debris")
local Force = Ball:GetMass() * 750 -- You can change 750
function onMouseClick()
local BodyAV = Instance.new("BodyAngularVelocity", Ball)
BodyAV.AngularVelocity = Ball.CFrame.LookVector * game.Workspace.Gravity * Force
Debris:AddItem(BodyAV, 0.5)
end
ClickDetector.MouseClick:Connect(onMouseClick)