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:

1local part = script.Parent
2local target_position = Vector3.new(0, 0, 0) -- for example
3 
4local body_position = Instance.new("BodyPosition")
5body_position.Position = target_position
6body_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:

1local part = script.Parent
2local target_position = Vector3.new(0, 0, 0) -- for example
3local force_factor = 50 -- fiddle with this to your liking
4 
5local body_force = Instance.new("BodyForce")
6body_force.Force = (target_position - part.Position) * force_factor * part:GetMass()
7body_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!

01local Workspace = workspace or game:GetService("Workspace")
02local Players = game:GetService("Players")
03local RunService = game:GetService("RunService")
04local Debris = game:GetService("Debris")
05local UserInputService = game:GetService("UserInputService")
06 
07local LocalPlayer = Players.LocalPlayer
08local Character = LocalPlayer.Character
09local Mouse = LocalPlayer:GetMouse()
10 
11local function LaunchProjectile(Origin)
12    local Projectile = Instance.new("Part",Character)
13    Projectile.Anchored = true
14    Projectile.CanCollide = false
15    Projectile.BrickColor = BrickColor.new("White")
View all 75 lines...
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.

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

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