I want my archer to shoot up and falls back down to the enemy. I’m using FastCast for this and I don’t know how I can achieve this.
Right now, my archer shoots straight but I want it to shoot up. It also misses.
local function GetPredictedPosition(targetPosition, targetVelocity, shooterPosition, shooterVelocity)
local gravity = ToolData.EffectGravity
local projectileSpeed = ToolData.BulletSpeed
local distance = (targetPosition - shooterPosition).Magnitude
local p = targetPosition - shooterPosition
local v = targetVelocity - shooterVelocity
local a = Vector3.new()
local timeTaken = (distance / projectileSpeed)
if gravity > 0 then
local timeTaken = projectileSpeed/gravity+math.sqrt(2*distance/gravity+projectileSpeed^2/gravity^2)
end
local goalX = targetPosition.X + v.X*timeTaken / gravity * a.X * timeTaken^2
local goalY = targetPosition.Y + v.Y*timeTaken / gravity * a.Y * timeTaken^2
local goalZ = targetPosition.Z + v.Z*timeTaken / gravity * a.Z * timeTaken^2
return Vector3.new(goalX, goalY, goalZ)
end
local nextPosition = GetPredictedPosition(targetPosition, targetVelocity, shooterPosition, shooterVelocity)
script.FireProjectile:Fire(firePoint, nextPosition)
FastCast Script:
local FastCast = require(Modules:WaitForChild("FastCastRedux"))
local caster = FastCast.new()
local castParams = RaycastParams.new()
castParams.FilterType = Enum.RaycastFilterType.Blacklist
castParams.IgnoreWater = true
local castBehavior = FastCast.newBehavior()
castBehavior.RaycastParams = castParams
castBehavior.Acceleration = Vector3.new(0, -ToolData.EffectGravity, 0)
castBehavior.AutoIgnoreContainer = false
castBehavior.CosmeticBulletContainer = projectilesFolder
castBehavior.CosmeticBulletTemplate = projectile
FastCast.VisualizeCasts = true
script.Parent.FireProjectile.Event:Connect(function(firePoint, hitPosition)
castParams.FilterDescendantsInstances = {character, projectilesFolder}
local orgin = firePoint.WorldPosition
local direction = (hitPosition - orgin).Unit
caster:Fire(orgin, direction, ToolData.BulletSpeed, castBehavior)
end)
Run the fastcast raycasts without any wait in between them so that they go instantly, and then take the fastcast raycast result, and base your target effects from there
I mean in the fastcast module itself. create a new SimulateCast function in the ActiveCast module that does essentially the same thing, but there’s no time-stepping
Check FastCast’s codebase and see if they handle projectile motion with the likes of a quadratic equation somewhere. If they solve that quadratic, you can change the quadratic formula such that it adds the discriminant ( + sqrt(b^2-4*a*c) ) rather than subtracting it and it should give you the high-arc motion you’re looking for.
local function GetPositionAtTime(time: number, origin: Vector3, initialVelocity: Vector3, acceleration: Vector3): Vector3
local force = Vector3.new((acceleration.X * time^2) / 2,(acceleration.Y * time^2) / 2, (acceleration.Z * time^2) / 2)
return origin + (initialVelocity * time) + force
end
-- A variant of the function above that returns the velocity at a given point in time.
local function GetVelocityAtTime(time: number, initialVelocity: Vector3, acceleration: Vector3): Vector3
return initialVelocity + acceleration * time
end
local function GetTrajectoryInfo(cast: ActiveCast, index: number): {[number]: Vector3}
assert(cast.StateInfo.UpdateConnection ~= nil, ERR_OBJECT_DISPOSED)
local trajectories = cast.StateInfo.Trajectories
local trajectory = trajectories[index]
local duration = trajectory.EndTime - trajectory.StartTime
local origin = trajectory.Origin
local vel = trajectory.InitialVelocity
local accel = trajectory.Acceleration
return {GetPositionAtTime(duration, origin, vel, accel), GetVelocityAtTime(duration, vel, accel)}
end
local function GetLatestTrajectoryEndInfo(cast: ActiveCast): {[number]: Vector3}
assert(cast.StateInfo.UpdateConnection ~= nil, ERR_OBJECT_DISPOSED)
return GetTrajectoryInfo(cast, #cast.StateInfo.Trajectories)
end