[THIS PART IS SOLVED]
Im trying to make a custom ball system for a soccer game (where you play as bunnies) And my ball goes in random zigzags, i want to make it so the ball can smoothly roll instead of going the exact opposite way but im not sure how to do that.
My ball logic:
local BounceAmount = .65
local OldGrounded = false
local GroundPosition = math.huge
local LastPos = Vector3.zero
local AirBegin = tick()
game["Run Service"].Heartbeat:Connect(function(dt)
BallPhysicsEnabled = not (BallHolder)
if BallPhysicsEnabled then
local vel:Vector3 = Ball:GetAttribute("Velocity")
local velWithoutY = Vector3.new(vel.X,0,vel.Z)
local params = RaycastParams.new(); params.FilterType = Enum.RaycastFilterType.Include; params.FilterDescendantsInstances = {workspace.Field}
local rayPos = Ball.Position-Vector3.new(0,Ball.Size.Y/1.95,0)
local CheckResolution = Ball.Size.Y/8
local AirTime = tick()-AirBegin
local rayDir = vel
local movementRay = workspace:Raycast(rayPos,rayDir,params)
if movementRay then
local normal =movementRay.Normal if vel.Magnitude < 2 then normal*=Vector3.new(0,0,0) end
vel *= (vel.Unit-normal).Unit*BounceAmount
func:PlaySound(SoundService.SFX.BallGround,Ball)
end
velWithoutY = Vector3.new(vel.X,0,vel.Z)
if math.abs(vel.Y) <= 0.0024 then
vel = velWithoutY
end
local gRay = workspace:Raycast(rayPos,Vector3.new(0,-5000,0),params); local dist; if gRay then dist = gRay.Distance GroundPosition=gRay.Position.Y else dist = math.huge end
Grounded = dist <= CheckResolution
if not Grounded then
vel -= Vector3.new(0,.0025,0)
vel = Vector3.new(vel.X,math.clamp(vel.Y,-CheckResolution,math.huge),vel.Z)
if not OldGrounded then AirBegin = tick() end
end
Ball.Position += vel; Ball.Position = Vector3.new(Ball.Position.X,math.clamp(Ball.Position.Y,GroundPosition+Ball.Size.Y/2,math.huge),Ball.Position.Z)
Ball:SetAttribute("Velocity",velWithoutY:Lerp(Vector3.new(0,0,0),.005)+Vector3.new(0,vel.Y,0))
OldGrounded = Grounded
else
Ball:SetAttribute("Velocity",Vector3.zero)
end
if BallHolder then
Ball.Position = BallHolder.HumanoidRootPart.BallPosition.WorldPosition
end
local v:Vector3 = (LastPos-Ball.Position)/2
Ball.CFrame *= CFrame.Angles(v.Z,0,v.X);
LastPos = Ball.Position
end)
Im trying to make a custom ball system for a soccer game (where you play as bunnies) And my ball goes in random zigzags, i want to make it so the ball can smoothly roll instead of going the exact opposite way but im not sure how to do that.
My ball logic:
local BounceAmount = .65
local OldGrounded = false
local GroundPosition = math.huge
local LastPos = Vector3.zero
local AirBegin = tick()
game["Run Service"].Heartbeat:Connect(function(dt)
BallPhysicsEnabled = not (BallHolder)
if BallPhysicsEnabled then
local vel:Vector3 = Ball:GetAttribute("Velocity")
local velWithoutY = Vector3.new(vel.X,0,vel.Z)
local params = RaycastParams.new(); params.FilterType = Enum.RaycastFilterType.Include; params.FilterDescendantsInstances = {workspace.Field}
local rayPos = Ball.Position-Vector3.new(0,Ball.Size.Y/1.95,0)
local CheckResolution = Ball.Size.Y/8
local AirTime = tick()-AirBegin
local rayDir = vel
local movementRay = workspace:Raycast(rayPos,rayDir,params)
if movementRay then
local normal =movementRay.Normal if vel.Magnitude < 2 then normal*=Vector3.new(0,0,0) end
vel *= (vel.Unit-normal).Unit*BounceAmount
func:PlaySound(SoundService.SFX.BallGround,Ball)
end
velWithoutY = Vector3.new(vel.X,0,vel.Z)
if math.abs(vel.Y) <= 0.0024 then
vel = velWithoutY
end
local gRay = workspace:Raycast(rayPos,Vector3.new(0,-5000,0),params); local dist; if gRay then dist = gRay.Distance GroundPosition=gRay.Position.Y else dist = math.huge end
Grounded = dist <= CheckResolution
if not Grounded then
vel -= Vector3.new(0,.0025,0)
vel = Vector3.new(vel.X,math.clamp(vel.Y,-CheckResolution,math.huge),vel.Z)
if not OldGrounded then AirBegin = tick() end
end
Ball.Position += vel; Ball.Position = Vector3.new(Ball.Position.X,math.clamp(Ball.Position.Y,GroundPosition+Ball.Size.Y/2,math.huge),Ball.Position.Z)
Ball:SetAttribute("Velocity",velWithoutY:Lerp(Vector3.new(0,0,0),.005)+Vector3.new(0,vel.Y,0))
OldGrounded = Grounded
else
Ball:SetAttribute("Velocity",Vector3.zero)
end
if BallHolder then
Ball.Position = BallHolder.HumanoidRootPart.BallPosition.WorldPosition
end
local v:Vector3 = (LastPos-Ball.Position)/2
Ball.CFrame *= CFrame.Angles(v.Z,0,v.X);
LastPos = Ball.Position
end)