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

How to make a part move in a direction forever?

Asked by 4 years ago
Edited 4 years ago

So, i'm improving my gun script and I need a bullet to move forward facing the position of anyCFrame.p. I need help lol.

0
when a player fires is it where the gun is facing or where the person click where the bullet goes to raid6n 2196 — 4y

4 answers

Log in to vote
1
Answered by
gskw 1046 Moderation Voter
4 years ago
Edited 4 years ago

You can use a BodyPosition to make a Part move towards a certain position. When using it, gravity doesn't affect the part.

For example:

local part = script.Parent
local target_position = Vector3.new(0, 0, 0) -- for example

local body_position = Instance.new("BodyPosition")
body_position.Position = target_position
body_position.Parent = part

You can also set the other properties (MaxForce, D for dampening and P for power) to take further control over the behaviour.

See the documentation here: https://developer.roblox.com/en-us/api-reference/class/BodyPosition

Note that when using a BodyPosition, the Part will stop once it has reached the specified position. You can use a BodyForce instead to move in a certain direction:

local part = script.Parent
local target_position = Vector3.new(0, 0, 0) -- for example
local force_factor = 50 -- fiddle with this to your liking

local body_force = Instance.new("BodyForce")
body_force.Force = (target_position - part.Position) * force_factor * part:GetMass()
body_force.Parent = part

Docs here: https://developer.roblox.com/en-us/api-reference/class/BodyForce

More information about BodyMovers here: https://developer.roblox.com/en-us/articles/BodyMover

Ad
Log in to vote
2
Answered by 4 years ago

Here's an awesome projectile script I made! It is completely functional under a LocalScript but you can work to configure it so that it replicates it to the server!

local Workspace = workspace or game:GetService("Workspace")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Debris = game:GetService("Debris")
local UserInputService = game:GetService("UserInputService")

local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character
local Mouse = LocalPlayer:GetMouse()

local function LaunchProjectile(Origin)
    local Projectile = Instance.new("Part",Character)
    Projectile.Anchored = true
    Projectile.CanCollide = false
    Projectile.BrickColor = BrickColor.new("White")
    Projectile.FormFactor = 3
    Projectile.Name = ("Projectile")
    Projectile.Material = Enum.Material.Neon
    Projectile.Size = Vector3.new(1,1,1)
    Projectile.Transparency = 0
    Projectile.TopSurface = 0
    Projectile.BottomSurface = 0
    Origin = (typeof(Origin) == "Vector3" and CFrame.new(Origin)) or (typeof(Origin) == "CFrame" and Origin)
    Projectile.CFrame = Origin
    local Scale = .1
    local PositionId = 0
    --coroutine.resume(coroutine.create(function()
        Projectile.Anchored = false
        local DirectionalPart = Instance.new("Part",Workspace)
        DirectionalPart.Anchored = true
        DirectionalPart.CanCollide = false
        DirectionalPart.Name = ("Direction")
        DirectionalPart.Material = Enum.Material.Neon
        DirectionalPart.Transparency = 1
        local RayObject = Ray.new(Origin.Position,(Mouse.Hit.Position - Projectile.CFrame.Position).Unit * 500);
        local Hit,Position,Normal = Workspace:FindPartOnRay(RayObject,Projectile)
        DirectionalPart.BottomSurface = 10
        DirectionalPart.TopSurface = 10
        local Distance = (Projectile.CFrame.Position - Position).Magnitude
        DirectionalPart.Size = Vector3.new(.1,.1,.1)
        DirectionalPart.CFrame = CFrame.new(Projectile.CFrame.Position,Position) * CFrame.new(0,0,0)
        Projectile.CFrame = DirectionalPart.CFrame
        DirectionalPart:Destroy()
        local BodyVelocity = Instance.new("BodyVelocity")
        BodyVelocity.MaxForce = Vector3.new(1e9,1e9,1e9)
        BodyVelocity.Velocity = Projectile.CFrame.LookVector * 125
        BodyVelocity.Parent = Projectile
        local HitDebounce = false
        Debris:AddItem(Projectile,15)
        wait()
        local HitEvent = Projectile.Touched:Connect(function(Hit)
            if HitDebounce == true then
                return
            end
            HitDebounce = true
            --[[
                Do hit stuff here.
            ]]
            HitDebounce = false
            Projectile.Anchored = true
            Projectile.Transparency = 1
            wait(8)
            Projectile:Destroy()
        end)
    --end))
end

UserInputService.InputBegan:Connect(function(InputObject,GPE)
    if GPE then
        return
    end
    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 and InputObject.UserInputState == Enum.UserInputState.Begin then
        LaunchProjectile(Character.Head.CFrame * CFrame.new(0,3,0))
    end
end)
Log in to vote
1
Answered by
uhi_o 417 Moderation Voter
4 years ago

raid6n had the correct version of this code but I'm just adding CFrames to it since that's what you wanted.

while true do
    local part = script.Parent
    part.CFrame= CFrame.new(part.Position) * CFrame.new(0,0,1) --the number depends on which direction it's going
end

You can also use lookVectors to make it continue in the direction it's going but I'm not so good with those.

Log in to vote
1
Answered by
iuclds 720 Moderation Voter
4 years ago

Simply lerp the parts CFrame in a loop such as a Heartbeat loop or a While loop.

Answer this question