So I want to tween a part into the mouse’s position, but I don’t know how to do it.
I was thinking of getting the mouse’s position then somehow tween it into that position you clicked at.
I don’t have any code because i’m still not sure how to do it, (and I am at school, using my free time to ask), its for a rocket launcher, and I am getting a part from ServerStorage and duplicate it into the workspace where the rocket launcher is. I can figure out the explosions myself.
well you wouldn’t necessarily want to tween for a rocker launcher or any gun-type system, but if you did you could do something along the lines of
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.ButtonOneDown:Connect(function()
—[[ use a raycast to hitscan the projectile.
you can use “mouse.Hit.Position” to send the
projectile in that direction]]—
end)
if you need more help check out this article about raycasting. or watch this video by TheDevKing. best of luck.
local player = game.Players.LocalPlayer
local Mouse = player:GetMouse() --Deprecated but still functions
local MousePos = Vector3.new(Mouse.Hit.X, 0, Mouse.Hit.Z)
game:GetService("UserInputService").InputBegan:Connect(function(i, gpe)
if not gpe and i.UserInputType == Enum.UserInputType.MouseButton1 then
print("TWEENING")
local INF = TweenInfo.new(
1,
Enum.EasingStyle.Sine,
Enum.EasingDirection.In,
0,
false,
0
)
local ENF = {["Position"] = MousePos}
game:GetService("TweenService"):Create(game.Workspace.Part, INF, ENF):Play()
end
end)
local run = game:GetService("RunService")
local tweens = game:GetService("TweenService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()
local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspace
mouse.TargetFilter = part
run.RenderStepped:Connect(function()
local tween = tweens:Create(part, TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Position = mouse.Hit.Position + Vector3.new(0, part.Size.Y/2, 0)})
tween:Play()
end)
Also i tried this out on another test script, this is kinda what I meant? But however, I was going for it to happen serversided, and I don’t want it following your mouse, I want it to move to where your mouse HIT. But I think I can fix that, but I am not sure
You could also use Raycasting on UserInputService, which will work generally better as it will be able to work more efficiently with Mobile + PC players with more compatibility.
local Cam = workspace.CurrentCamera
local Parameters = RaycastParams.new()
Parameters.FilterDescendantsInstances = {} -- Place in here if there is a specific group you want to be filtered (whitelisted or blacklisted)
Parameters.FilterType = Enum.RaycastFilterType.Whitelist -- This will tell the raycast whether to whitelist or blacklist the filter.
local function RayResult(x, y)
local unitRay = Cam:ScreenPointToRay(x, y) -- You could also use viewportpointtoray
return workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, Parameters)
end
-- You could then recall the position of it by doing
game:GetService("UserInputService").InputBegan:Connect(function(Input, Processed)
if not Processed then
if Input.UserInputType == Enum.UserInputType.Touch or Input.UserInputType == Enum.UserInputType.MouseButton1 then
local Result = RayResult(Input.Position.X, Input.Position.Y)
local Position = Result.Position -- This would then put the world space point where the mouse was into a variable as a Vector3, you can then use that to tweenposition easily.
end
end
end)
The script I posted I had limited to whitelist, but you hadn’t added anything into the descendants. I’ve changed the filter type to Blacklist here and added a if statement checking if the result exists before it carries out any task to reduce errors.
local Cam = workspace.CurrentCamera
local Parameters = RaycastParams.new()
Parameters.FilterDescendantsInstances = {} -- Place in here if there is a specific group you want to be filtered (whitelisted or blacklisted)
Parameters.FilterType = Enum.RaycastFilterType.Blacklist -- This will tell the raycast whether to whitelist or blacklist the filter.
local function RayResult(x, y)
local unitRay = Cam:ScreenPointToRay(x, y) -- You could also use viewportpointtoray
return workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, Parameters)
end
-- You could then recall the position of it by doing
game:GetService("UserInputService").InputBegan:Connect(function(Input, Processed)
if not Processed then
if Input.UserInputType == Enum.UserInputType.Touch or Input.UserInputType == Enum.UserInputType.MouseButton1 then
local Result = RayResult(Input.Position.X, Input.Position.Y)
if Result then
print(Result.Position)
end
end
end
end)
Instead of printing the position you could fire it to the server for the server to act with any form of movement you need.