# How to make a projectile with RayCast be able to be fired into the sky?

Hello, I’m trying to make a projectile able to be fired into the sky using raycast, but right now it only can fire towards the ground or an object. Here’s my script, (newFF is the ability’s cloned part):

``````		local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local raycastResult = workspace:Raycast(newFF.Position, (mousePos - newFF.Position) * 300, raycastParams)

if raycastResult then
newFF.CFrame = CFrame.new(newFF.Position, raycastResult.Position)

local bv = Instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.Parent = newFF
newFF.Anchored = false
bv.Velocity = newFF.CFrame.LookVector * speed
end
``````

Wdym by “right now it only can fire towards the ground or an object”, could you elaborate more on that? Since if the projectile path was straight, wouldn’t you be able to aim at the sky? It would be great if you could elaborate more on how the current system works and why it can’t do what you are wanting it to do.

I meant that it can only fire towards the ground or objects, and it doesn’t work when I aim at the sky

Is the object falling down when you try to aim at the sky? If so, did you try adding more force?

The object isn’t falling down, it remains stuck. For the force, I assume that it’s already at max force since I added math.huge for the parameters of BodyVelocity.

The RayCast is unable to reach the sky, so the object doesn’t move. I want to find some way so that even if the RayCast is unable to reach the sky, the object moves in the direction of it.

Oh, I see what you mean now. You can add this line that will calculate the intersection when ray doesn’t hit anything, and will return the actual intersection position when the ray does hit something.

``````local intersection = result and result.Position or origin + direction * 300
``````

this `and` that `or` something is similar to `if` this `then` that `else` something

If you need distance, you can calculate it too.

``````local distance = (origin - intersection).Magnitude
``````

Therefore your script would be something like this:

``````local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local origin = newFF.Position
local direction = (mousePos - newFF.Position) * 300

local result = workspace:Raycast(origin, direction, raycastParams)

local intersection = result and result.Position or origin + direction

newFF.CFrame = CFrame.new(newFF.Position, intersection)

local bv = Instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.Parent = newFF
newFF.Anchored = false
bv.Velocity = newFF.CFrame.LookVector * speed
``````
3 Likes