Hello im trying to Create a Trajectory System Where as if the Ball is touched its launches off and a Trajectory is Calculated based on where the ball is Here is My Current Code and how I handle this now but it doesn’t seem to be feasible. How can I improve this with Fast Cast as I’ve heard its Cleaner and More Optimised for these type of things.
Here is something similar of what I’m trying to achieve.
But With my Ball How it works i that it targets a specific player and kill them if their not blocking, but i ant if the player is blocking then its launched off bounces around the map like a trajectory and then targets a new player. Here is my Current Setup:
Module Code:
local Settings = {}
local Event = game.ReplicatedStorage.GameEvent
local Ball: Part
function Settings:GetTarget()
local Players = game.Players:GetPlayers()
local rand = math.random(#Players,1)
for i, v in pairs(Players) do
if i == rand then
rand = v
end
end
return rand
end
function SpawnAndTrack()
local Target = Settings:GetTarget()
Ball:SetNetworkOwner(Target)
Event:FireAllClients(Target)
end
Event.OnServerEvent:Connect(function()
SpawnAndTrack()
end)
function Settings:Start()
if not workspace:FindFirstChild("Ball") then
Ball = script.Ball
Ball = Ball:Clone()
Ball.Parent = workspace
Ball.Position = Vector3.new(0,0,0)
end
SpawnAndTrack()
end
return Settings
Here is My Client Code:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Event = game.ReplicatedStorage.GameEvent
local RunService = game:GetService("RunService")
local Config = require(game.ReplicatedStorage.MainGameConfig)
local TweenService = game:GetService("TweenService")
local Physics = game:GetService("PhysicsService")
function ChangeBallColors(Ball: Part,Color: Color3)
for i,v: ParticleEmitter in pairs(Ball.Attachment:GetChildren()) do
v.Color = ColorSequence.new(Color)
end
end
local Color =
{
Blue = Color3.new(0, 0, 1),
Red = Color3.new(1, 0, 0),
Grey = Color3.new(0.666667, 0.666667, 1)
}
Event.OnClientEvent:Connect(function(Target: Player)
local Ball: Part = workspace.Ball
-- Change Color Based on Target
if Player.Name == Target.Name then
ChangeBallColors(Ball,Color.Red)
else
ChangeBallColors(Ball,Color.Grey)
end
-- TrackTarget
local TargetCharacter = Target.Character
local function updateBallPosition()
if TargetCharacter and TargetCharacter.PrimaryPart then
local targetPosition = TargetCharacter.PrimaryPart.Position
local direction = (targetPosition - Ball.Position).unit -- Calculate direction vector
local speed = 120 -- Adjust the speed as needed
-- Move the ball towards the player
Ball.Position = Ball.Position + direction * speed * RunService.Heartbeat:Wait()
else
Ball:Destroy()
end
end
local connection
connection = RunService.RenderStepped:Connect(updateBallPosition)
Ball.Touched:Connect(function(hit)
if hit.Parent.Name == Target.Name then
if TargetCharacter.Blocking.Value == true then
connection:Disconnect()
local knockbackForce = 2 -- Adjust the knockback force as needed
-- Calculate knockback direction
local knockbackDirection = (Ball.Position - hit.Parent.PrimaryPart.Position).unit
-- Apply impulse to the ball
local knockbackImpulse = knockbackDirection * knockbackForce
Physics:SetPartCollisionGroup(Ball, "NonCollidable") -- Temporarily disable collisions
Ball.Velocity = Ball.Velocity + knockbackImpulse
task.wait(0.1) -- Delay before restoring collisions
Physics:SetPartCollisionGroup(Ball, "Default") -- Restore collisions
Event:FireServer()
else
TargetCharacter.Humanoid.Health = 0
end
end
end)
end)
The Current Calcualtions are done on the Client in the Touched Event