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

How do i make something move towards the mouse?

Asked by 4 years ago

So basically i am trying to make a airsoft gun. I have almost finished, now i only need to make the pellets move towards the mouse. I tried several methods of how making the pellets move to where the mouse is, but it never works. Help?

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Bulletfly")
local Bulletscript = repStorage:WaitForChild("BulletScript")

remote.OnServerEvent:connect(function(player, f, player)
    local IAMAPEWPEW = Instance.new("Part", game.Workspace.Pellets)
    IAMAPEWPEW.Name = "Pellet"
    IAMAPEWPEW.Material = "Plastic"
    IAMAPEWPEW.BrickColor = BrickColor.new("Institutional white")
    IAMAPEWPEW.Size = Vector3.new(0.1,0.1,0.1)
    IAMAPEWPEW.Shape = "Ball"
    IAMAPEWPEW.Position = game.Workspace[player].Gun.Handle.Position

    --bulletscript
    local copy = Bulletscript:Clone()
    copy.Disabled = false
    copy.Parent = IAMAPEWPEW
    copy.Origin.Value = player
end)
0
I think you need to show us what is in the "Bullet Script" Azure_Kite 885 — 4y
0
In the "Bullet Script" is just a script that detects if there is a humanoid, checks if it has the player name, and if not it kills the humanoid david15557 5 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

What you need to do, is make sure this bullet is not anchored (By default an instance that was created comes unanchored) and add a body velocity!

But to make something move towards the mouse, we must have the must have the mouse position, so in the local script that you are firing that event from you must include a parameter containing the position of the mouse.

After getting it, you have to create a BodyVelocity inside the bullet and make it move towards the mouse position!

To do it, you must play with the Velocity of the BodyVelocity.

But what is a bullet without spread? I've added a calculation to make the bullet spread as much as you'd like, you just need to change the value of "Spread".

If you want the bullet to go fast/slow, you just need to change the "300" value that I included in the script to something like 150, 100. Depends on the gun really!

To finish, you must add something to destroy the bullet after a bit if it doesn't hit anything. I've used the Debris system.

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Bulletfly")
local Bulletscript = repStorage:WaitForChild("BulletScript")

remote.OnServerEvent:connect(function(player, f, player, MousePosition)
    local IAMAPEWPEW = Instance.new("Part", game.Workspace.Pellets)
    IAMAPEWPEW.Name = "Pellet"
    IAMAPEWPEW.Material = "Plastic"
    IAMAPEWPEW.BrickColor = BrickColor.new("Institutional white")
    IAMAPEWPEW.Size = Vector3.new(0.1,0.1,0.1)
    IAMAPEWPEW.Shape = "Ball"
    IAMAPEWPEW.Position = game.Workspace[player].Gun.Handle.Position

    local BV = Instance.new("BodyVelocity",IAMAPEWPEW)
    BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)


----------------------------------------------------------------------------
    local Spread = 0.005 --Change this to change the spread

    local targetPoint = MousePosition
    local shootDirection = (targetPoint - game.Workspace[player].Gun.Handle.Position).unit
          shootDirection = CFrame.Angles((0.5 - math.random()) * 7 * Spread,
                                                                (0.5 - math.random()) * 7 * Spread,
                                                                (0.5 - math.random()) * 7 * Spread) * shootDirection
----------------------------------------------------------------------------


    BV.Velocity = shootDirection * 300 --Change "300" to set how fast you want the bullet to be.


    game:GetService("Debris"):AddItem(a,3)   --Change 3 to the anmount of time to wait until the bullet despawns

end)

Tell me if you find any problems!

0
There is something wrong with "local shootDirection = (targetPoint - game.Workspace[player].Gun.Handle.Position).unit". The c onsole says "bad argument #1 (Vector3 expected, got nil)" david15557 5 — 4y
Ad

Answer this question