Projectile defying gravity

I have a script that throws the block to where the character is facing, but the problem is that the projectile does not shoot and it falls, I want it so that it shoots from humanrootpart and fire straight defying gravity at where the player is facing…what I have so far:

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local camera = workspace.CurrentCamera
local Player = Players.LocalPlayer
local pathToThePart = ReplicatedStorage.Part

UIS.InputBegan:Connect(function(Key, Chatted)
	if Chatted then
		return
	end
	if Key.KeyCode == Enum.KeyCode.E then
		local projectile = pathToThePart
		projectile.Parent = workspace
		projectile.CFrame = Player.Character.HumanoidRootPart.CFrame
		projectile.Velocity = camera.CFrame.LookVector * 100
	end
end)

There are three ways

  1. Use BodeVelocity to set the velocity so it doesn’t fall.
  2. Use Raycasts and tween the bullet s position
  3. Use maths to calculate the required Vector Force you need upward by doing workspace.Gravity *bullet:GetMass()

another question…my game is 2d so the look victor is facing the background…how do i change it so that it fires at where the player 's humanrootpart(other bodypart) is facing?

How would i modify the script to do that?

Hmm, well I am not entirely sure what you are asking. But there is a way to get the vector from one position to another position or direction.

(PositionA - PositionB).Unit is a direction from PositionB to PositionA

like how would i change the look victor to the humanrootpart’s lookvictor(or the charcter’s facing look victor)

Hmm. Do you know exactly what a look vector is?

player’s camera facing diraction?

Well yes. However, they are read-only that means they cannot be changed specifically.

so like this?:

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local camera = workspace.CurrentCamera
local Player = Players.LocalPlayer
local pathToThePart = ReplicatedStorage.Part

UIS.InputBegan:Connect(function(Key, Chatted)
	if Chatted then
		return
	end
	if Key.KeyCode == Enum.KeyCode.E then
		local projectile = pathToThePart
		projectile.Parent = workspace
		projectile.CFrame = Player.Character.HumanoidRootPart.CFrame
		projectile.Velocity = Player.Character.HumanoidRootPart.CFrame
end)

Hmm not quite. The way to use body velocities is actually the most easiest to do.

  1. Create a BodyVelocity in the part you want to move.
  2. Set the BodyVelocity.MaxForce to math.huge or ‘inf’
  3. Set the BodyVelocity.Velocity to the direction * speed

CFrame.lookVector is a direction.

Because of this you can not directly assign it unless you use CFrame.fromMatrix() which I highly recommend you don’t use until you fully understand CFrame

i can only do 0 not * ;-;

Any number that is big will do. You don’t have to use ‘inf’ just any number that is big should be able to lift the bullet from the ground like 100000

what about the third step… i can’t do *

Look you did it here. However instead of setting it to the projectiles velocity set it to the BodyVelocity’s Velocity instead

ok but still how would i make the projectile fire at where the humanrootpart is facing?

HumanoidRootPart.CFrame.lookVector * speed

Remember look vector is a direction.

Hope this helps!

hmmmmm…how would i make it so that the part become ancord when touched a part?

btw i tried this:

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local camera = workspace.CurrentCamera
local Player = Players.LocalPlayer
local pathToThePart = ReplicatedStorage.Part

UIS.InputBegan:Connect(function(Key, Chatted)
	if Chatted then
		return
	end
	if Key.KeyCode == Enum.KeyCode.E then
		local projectile = pathToThePart
		projectile.Parent = workspace
		projectile.CFrame = Player.Character.HumanoidRootPart.CFrame
		projectile.BodyVelocity = Player.Character.HumanoidRootPart.CFrame.lookVector * 1000
	end
end)

and the projectile dosn’t fire… it just stay where my charcter is…