Hi again! I need help making a projectile that follows your mouse like in this video! I already made one but it doesn’t follow the mouse but anyways any help would be appreciated thank you!
local Speed = 5
Projectile.CFrame = CFrame.lookat(Projectile.Position, MousePosition) * CFrame.new(0, 0, -Speed)
(Note: You will have to keep track of your MousePosition constantly though)
like Mouse.Move for the track? or is there other way
you can use
game:GetService["Run Service"].Stepped:Connect(function()
--do stuff
end)
I tried that and it lags me so much
What code did you put in the connection? If I had to guess, there was no issue with the code, but you made a lot of projectiles and they all got destroyed, but you left the Stepped connected.
Also you can use GetMouseLocation from UserInputService to get the Mouse’s position, and then ViewportPointToRay to get the Vector3 of the position it’s pointing to.
Ex (using the previous two answers):
local UserInput = game:GetService("UserInputService")
local Camera = workspace.CurrentCamera
local Projectile = ... -- Assume this is defined, you can implement this as you wish.
local Speed = 5 -- We don't need to keep defining this
local ProjectileMover = game:GetService["Run Service"].Stepped:Connect(function()
local MouseV2 = UserInput:GetMouseLocation() -- Get the mouse's position in pixels
local MousePosition = Camera:ViewportPointToRay(MouseV2, 0) -- Get the 3D point with a raycast
Projectile.CFrame = CFrame.lookat(Projectile.Position, MousePosition) * CFrame.new(0, 0, -Speed) -- Update the Projectile's CFrame
end)
-- And don't forget an important step.
-- Once is so useful, it makes clean disconnects much easier.
Projectile.Destroying:Once(function()
ProjectileMover:Disconnect() -- Prevent memory leaks and spamming errors.
end)
I know from experience that AlignPosition gives you that type of movement
I tried this and it has an error “Unable to cast Vector2 to float.”
can you please tell us what line the error was on
the line it says “local MousePosition = Camera:ViewportPointToRay(MouseV2, 0)”
It’s supposed to be
local MousePosition = Camera:ViewportPointToRay(MouseV2.X, 0)
fixed but it doesnt even follow where the mouse go
Sorry, I was quite busy and ended up falling asleep for a while as well.
@DataSigh fixed half of it; thank you since I overlooked that entirely.
Though there’s still another parameter missing:
local MousePosition = Camera:ViewportPointToRay(MouseV2.X, MouseV2.Y, 0)
The 0 on the end just signifies the studs away from the camera, not super helpful if it’s offset by anything so leave it as is.
Try that and see if it works now.
Tried this and it doesnt follow the mouse like on the video