What do you want to achieve?
I want the projectile to continue forward when it loses the target.
What is the issue?
Projectile doesn’t go straight; targetVector becomes NaN
What solutions have you tried so far?
I have tried using Roblox’s AI assistant and researching, but I’ve come up with no true solutions.
The AI gave me this, it fixes the whole script breaking which I was having problems with before, though the projectile just veers off into a different direction instead of straight forward like it’s meant to.
If the targetVector happens to be equal to Vector3.zero, then attempting to turn it into a unit vector will result in NaN, so use an if statement to check if the targetVector is equal to Vector3.zero before converting it to a unit vector
@bonebreaker114@7z99 has a good point: if you want the raycast to fire in a straight line according to the direction the gun is facing, using this formula to calculate the target vector is recommended:
targetVector = CF.Position + CF.LookVector * 1000 -- Equivalent to saying 1000 studs in front of the projectile's current position
No, as this just breaks it since targetVector has to be a set coordinate; I’m attempting to set targetVector 1000 studs in the direction of the projectile CFrame.
If your code expects a unit vector for the direction that the gun is facing, then why not use the CF.LookVector directly? I don’t understand why you’re multiplying the look vector with a distance, as doing so will convert it to a regular vector
For the gun to shoot perfectly straight, you’ll need to do something like this:
local distance = 1000
local origin = barrel.Position -- Use .WorldPosition if using an Attachment
local direction = barrel.CFrame.LookVector -- Use .WorldCFrame.LookVector if using an Attachment
local raycastResult = workspace:Raycast(origin, direction * distance)
print(raycastResult)
@bonebreaker114 Note: if raycastResult is equal to nil, you can use origin + direction * distance to calculate the projectile’s target, otherwise use the raycastResult’s position
local targetPosition = if raycastResult then raycastResult.Position else origin + direction * distance
It’s meant to be a coordinate/vector3 that gets pushed a 1000 studs forward (in front of the projectile) each update when there’s no target until it either times out or finds a target.
Still wants to bend off into different directions instead of going straight, I’m lost too since I don’t know too much about this side of programming; still trying to get the hang of everything.
Using a function such as this one to handle moving the projectile should work:
local TweenService = game:GetService("TweenService")
local DISTANCE = 1000
local TWEEN_INFO = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
local function calculateProjectile(projectile: BasePart)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {projectile}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local thread = task.spawn(function()
while true do
local origin = projectile.Position
local direction = projectile.CFrame.LookVector
local raycastResult = workspace:Raycast(origin, direction * DISTANCE, raycastParams)
local targetPosition
if raycastResult then
-- The projectile has hit something
targetPosition = raycastResult.Position
else
targetPosition = origin + direction * DISTANCE
end
local tween = TweenService:Create(projectile, TWEEN_INFO, {Position = targetPosition})
tween:Play()
tween.Completed:Wait()
if raycastResult then break end -- Must break the loop once the projectile has hit something
end
end)
task.delay(4, function() -- After 4 seconds have passed, stop the loop and destroy the projectile
task.cancel(thread)
projectile:Destroy()
end)
end
@bonebreaker114 I’ve made an important edit to the code above: I forgot to create RaycastParams for the projectile, which can create issues if it has CanQuery set to true
Unfortunately, I’m not familiar with using FastCast, as I prefer writing code myself whenever possible, but if you continue to encounter issues with your code, then I recommend that you’ll consider my suggestion
I’ll still check FastCast’s docs to see if I can make any changes to the code in-order to make it work with it, though
@bonebreaker114 I’m back from doing research + testing FastCast, and by default, the caster:Fire function will shoot the ray straight unless you add a new behavior to edit the projectile’s acceleration
The current script that you’re using seems to be complicating things much more than they really need to be, since FastCast already has useful functionality built in to it