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.
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:
1 | local part = script.Parent |
2 | local target_position = Vector 3. new( 0 , 0 , 0 ) -- for example |
3 |
4 | local body_position = Instance.new( "BodyPosition" ) |
5 | body_position.Position = target_position |
6 | 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:
1 | local part = script.Parent |
2 | local target_position = Vector 3. new( 0 , 0 , 0 ) -- for example |
3 | local force_factor = 50 -- fiddle with this to your liking |
4 |
5 | local body_force = Instance.new( "BodyForce" ) |
6 | body_force.Force = (target_position - part.Position) * force_factor * part:GetMass() |
7 | 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
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!
01 | local Workspace = workspace or game:GetService( "Workspace" ) |
02 | local Players = game:GetService( "Players" ) |
03 | local RunService = game:GetService( "RunService" ) |
04 | local Debris = game:GetService( "Debris" ) |
05 | local UserInputService = game:GetService( "UserInputService" ) |
06 |
07 | local LocalPlayer = Players.LocalPlayer |
08 | local Character = LocalPlayer.Character |
09 | local Mouse = LocalPlayer:GetMouse() |
10 |
11 | local function LaunchProjectile(Origin) |
12 | local Projectile = Instance.new( "Part" ,Character) |
13 | Projectile.Anchored = true |
14 | Projectile.CanCollide = false |
15 | Projectile.BrickColor = BrickColor.new( "White" ) |
raid6n had the correct version of this code but I'm just adding CFrames to it since that's what you wanted.
1 | while 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 |
4 | end |
You can also use lookVectors to make it continue in the direction it's going but I'm not so good with those.
Simply lerp the parts CFrame in a loop such as a Heartbeat loop or a While loop.