In my script, it does a homing attack done in most sonic games. However, during this action, the homing attack ball is suppose to center at the torso through the whole sequence rather than trail behind the torso?

Any ideas how I can fix this?

local player = game:GetService("Players").LocalPlayer
mouse = player:GetMouse()
local jump = false
local charging = false
local homing = false
local speed = 100
local direction = Vector3.new(0, 0, 1)
local lastPosition = Vector3.new(0, 0, 0)
local originPosition = Vector3.new(0, 0, 0)
local lastTarget = nil
local velocity = Instance.new("BodyVelocity")
local spin = game.Workspace.Rushman:Clone()
spin.Name = "Spin"
spin.Anchored = true
spin.CanCollide = false
spin.BackSurface = 0
spin.BottomSurface = 0
spin.FrontSurface = 0
spin.LeftSurface = 0
spin.RightSurface = 0
spin.TopSurface = 0
spin.Transparency = 0
spin.Size = Vector3.new(3.57, 3.57, 3.57)
local hint = Instance.new("Hint")
hint.Text = "Spin Dash: Click and release | Homing attack: Double press spacebar"

function checkSit()
	if player.Character == nil then return true end
	if player.Character:FindFirstChild("Humanoid") == nil then return true end
	if player.Character.Humanoid.Sit == true then return true end
	return false
end

function transparent(value, object)
	if object == nil then return end
	object = object:children()
	if #object < 1 then return end
	for i = 1, #object do
		if object[i].className == "Part" and object[i].Name ~= "Spin" then object[i].Transparency = value else transparent(value, object[i]) end
	end
end

function onButton1Down()
	local torso = player.Character:FindFirstChild("Torso")
	if charging == true or torso == nil or homing == true or checkSit() == true then return end
	charging = true
	speed = 50
	spin.BrickColor = torso.BrickColor
	spin.Parent = player.Character
	for i = 1, 50 do
		if charging == false then return end
		if jump == true then break end
		speed = speed + 2
		spin.CFrame = CFrame.new(Vector3.new(0, 0, 0), direction) + torso.Position - Vector3.new(0, 1.01, 0)
		spin.CFrame = spin.CFrame * CFrame.fromEulerAnglesXYZ(-1 * i, 0, 0)
		if spin.Parent == nil then spin.Parent = player.Character end
		wait()
		if i == 1 then transparent(1, player.Character) end
	end
	if jump == true then transparent(1, player.Character) end
	onButton1Up()
end

function onButton1Up()
	local torso = player.Character:FindFirstChild("Torso")
	if torso == nil or jump == true or charging == false or checkSit() == true then return end
	charging = false
	lastPosition = torso.Position
	velocity.maxForce = Vector3.new(1e+005, 0, 1e+005)
	velocity.Parent = player.Character:FindFirstChild("Torso")
	for i = 1, speed do
		if jump == true or charging == true then break end
		if checkSit() == true then break end
		speed = speed - 1
		local trail = spin:clone()
		trail.Transparency = .8
		trail.TopSurface = 0
		trail.BottomSurface = 0
		trail.CFrame = torso.CFrame
		trail.Parent = player.Character
		game:service("Debris"):addItem(trail, 1)
		spin.CFrame = CFrame.new(Vector3.new(0, 0, 0), direction) + torso.Position - Vector3.new(0, 1.01, 0)
		spin.CFrame = spin.CFrame * CFrame.fromEulerAnglesXYZ(-1 * i, 0, 0)
		if spin.Parent == nil then spin.Parent = player.Character end
		velocity.velocity = direction * speed
		lastPosition = torso.Position
		if i == 1 then transparent(1, player.Character) end
		wait()
	end
	if jump == false or charging == false then
		velocity.Parent = nil
		spin.Parent = nil
		transparent(0, player.Character)
	end
end

function onKeyDown(key)
	key = key:lower()
	local torso = player.Character:FindFirstChild("Torso")
	if (key == " ") and jump == false and torso ~= nil then
		jump = true
		spin.BrickColor = BrickColor.new("Navy Blue")
		for i = 1, 15 do
			if homing == true or jump == false then break end
			spin.CFrame = torso.CFrame * CFrame.fromEulerAnglesXYZ(-.5 * i, 0, 0)
			if spin.Parent == nil then spin.Parent = torso end
			wait()
			if i == 1 then transparent(1, player.Character) end
		end
		if homing == false then spin.Parent = nil transparent(1, player.Character) end
		jump = false
	elseif (key == " ") and homing == false and torso ~= nil then
		homing = true
		jump = false
		transparent(1, player.Character)
		local target = nil
		local distance = 10
		local list = game.Workspace:children()
		for i = 1, #list do
			if list[i].className == "Part" and list[i].Size.magnitude < 6 and list[i] ~= lastTarget and (list[i].Position - torso.Position).magnitude < distance and list[i].Position.y < torso.Position.y then
				target = list[i]
				distance = (list[i].Position - torso.Position).magnitude
			end
		end
		if target ~= nil then
			lastTarget = target
			velocity.maxForce = Vector3.new(1e+005, 1e+005, 1e+005)
			velocity.Parent = torso
			transparent(1, player.Character)
			for i = 1, 10 do
				if jump == true or (torso.Position - target.Position).magnitude < 4 then break end
				spin.CFrame = torso.CFrame * CFrame.fromEulerAnglesXYZ(-.5 * i, 0, 0)
				if spin.Parent == nil then spin.Parent = torso end
				velocity.velocity = torso.CFrame.lookVector * 100
				wait(0)
			end
			spin.Parent = nil
			transparent(1, player.Character)
			homing = false
			velocity.maxForce = Vector3.new(1e+005, 1e+005, 1e+005)
			velocity.velocity = Vector3.new(0, 10, 0)
			wait(0)
			velocity.Parent = nil
		else
			originPosition = torso.Position
			lastTarget = nil
			velocity.maxForce = Vector3.new(1e+005, 1e+005, 1e+005)
			velocity.Parent = torso
			for i = 1, 10 do
				if (torso.Position - originPosition).magnitude > 30 or checkSit() == true then break end
				spin.CFrame = torso.CFrame * CFrame.fromEulerAnglesXYZ(-.5 * i, 0, 0)
				if spin.Parent == nil then spin.Parent = torso end
				velocity.velocity = torso.CFrame.lookVector * 100
				wait(0)
			end
			velocity.Parent = nil
			transparent(1, player.Character)
			wait(0)
			homing = false
			spin.Parent = nil
		end
	end
end


	if mouse == nil then return end
	if player:FindFirstChild("Message") ~= nil then player.Message.Parent = nil end
	hint.Parent = player
	mouse.KeyDown:connect(onKeyDown)
	mouse.Move:connect(function()
		if player.Character:FindFirstChild("Torso") ~= nil then
			direction = (mouse.Hit.p - player.Character:FindFirstChild("Torso").Position).unit
			direction = Vector3.new(direction.x, 0, direction.z)
		end
end)