Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Aiming a FE projectile?

Asked by 7 years ago

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.

1 answer

Log in to vote
4
Answered by 7 years ago
Edited 7 years ago

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

Ad

Answer this question