I am trying to make the characters torso point towards the mouse for a few moments then fire a projectile. The projectile is in a tool format and I have tried using TargetPoint, but that does not seem to be working. Any suggestions are helpful.
Aiming a FE projectile?
1 answer
First off, you would need to get the Player's Mouse. You can call the Player's Mouse from a local script using
local mouse = game.Players.LocalPlayer:GetMouse()
torso = workspace:FindFirstChild(game.Players.LocalPlayer.Name):FindFirstChild("Torso") --you need the torso too
Next you would need a function that makes the torso face towards where the bullet is aimed. I would suggest using BodyGyro. BodyGyro is an Instance that takes a CFrame value and points it's parent towards that CFrame value. (Only applies for parts)
function launch(pos) --pos is for later use
local gyro = Instance.new("BodyGyro", torso) --the second parameter is the parent
gyro.cframe = CFrame.new(torso.Position, pos.p)
end
Next you will need to fire a projectile, which can be done using BodyVelocity. Body Velocity is an Instance that takes a Vector3 value and sets that Vector3 value as its target velocity. So we would just need to feed it the mouse's Position, which will later be defined as pos.
local bullet = Instance.new("Part", workspace)
bullet.CanCollide = false
bullet.CFrame = torso.CFrame
local velocity = Instance.new("BodyVelocity", bullet)
velocity.Velocity = pos.p -- .p converts the CFrame to a Vector3 value
velocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge) --this keeps the bullet going at a constant rate
game:GetService("Debris"):AddItem(bullet, 3) --removes bullet after 3 seconds
gyro:Destroy()
Finally, you need to call your function. You need to call it every time you click, so you would use the event Button1Down.
mouse.Button1Down:connect(function()
launch(mouse.Hit)
end)
Put all of this together and you get
local mouse = game.Players.LocalPlayer:GetMouse()
torso = workspace:FindFirstChild(game.Players.LocalPlayer.Name):FindFirstChild("Torso")
function launch(pos) --pos is for later use
local gyro = Instance.new("BodyGyro", torso) --the second parameter is the parent
torso.BodyGyro.CFrame = CFrame.new(torso.Position, pos.p)
local bullet = Instance.new("Part", workspace)
bullet.CanCollide = false
bullet.CFrame = torso.CFrame
local velocity = Instance.new("BodyVelocity", bullet)
velocity.Velocity = pos.p -- .p converts the CFrame to a Vector3 value
velocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge) --this keeps the bullet going at a constant rate
game:GetService("Debris"):AddItem(bullet, 3) --removes bullet after 3 seconds
gyro:Destroy()
end
mouse.Button1Down:connect(function()
launch(mouse.Hit)
end)
Hope this helped! --connor12260311
This discussion is preserved as it was. New answers, comments, and votes are closed.
