I am trying to make a ball where, if the player touches it, the ball will kick forward like a soccer ball. I set all the CustomPhysicalProperties to 0 but I'm not getting the results I'm looking for.
Add a .Touched event to each of the player’s legs and if part it hit is the ball then Ball.Velocity = Ball.Velocity + CFrame.lookat(Leg.Position, Ball.Position).lookVector * Force
this is not exact code as it probably has syntax errors but it is a start
As the player touches the ball, save the velocity of the players part that touched the ball, and then considering it give velocity to the ball in the opposite direction. You may use BodyForce but this thing is somewhat laggier than simple velocity.
Btw, there is some stuff with Custom Physics Properties in the part properties, you might wanna have a look.
How would I go about having a function connected to the player’s legs that causes the ball to launch forwards? Can I tell the ball to function onHit, or is there more to that? I also dont know if I want to have it only function when it hits the legs, I think it would be better for me if the ball would go forward no matter where you hit it.
I mean, if, for instance the part of the player was his foot - and its velocity was, for example, (0, 15, 24), you just take this velocity and apply it to the part, but since a human leg irl has more mass you just multiply the velocity by, idk, may be 5?
Roblox is its own physics engine and im sure you wouldnt want them scoring exactly where they clicked, that’d be unfair so we dont need fancy orbital physics or projectile motions you can just use the roblox physics engine and apply a body force to the soccer ball according the the lookvector of the player when they touched it
The way Roblox soccer games usually go on to do this (TPS: Ultimate Soccer, RES) is by parenting a BodyVelocity to the ball, with the velocity property of the BodyVelocity being set to the leg CFrame look vector.
They usually parent the body velocity inside the ball for 0.3 seconds, when .Touched event fires and a few checks passes inside the .Touched event.
However, you can use the recently released BasePart:ApplyImpulse() methods that I believe achieve a far better result instead, rather than using a legacy body mover.
I’ll show an pseudo-code example for this, which you could work off:
local Power = ???
RightLeg.Touched:Connect(function(hit: BasePart)
-- May be terrain, so we check that it is actually an part and not just a BasePart.
if not hit:IsA("Part") then
return
end
-- Check if hit is actually a ball.
if hit.Shape ~= Enum.PartType.Ball then
return
end
-- Then apply the impulse:
hit:ApplyImpulse(RightLeg.CFrame.LookVector * power * hit:GetMass())
end)
With a fast moving game like soccer, you are going to need accurate hit detection for your kicks. I recommend using raycast-hitboxing to do this. Once you detect a hit, simply add a bodyforce to your soccer ball.
I am not using it for soccer. It’s a different type of game. In this game you do not need concistancy with the ball, you just need to get the ball away from yourself.
The code I provided is pseudo-code (well, not necessarily since I still wrote it in a way if you filled out the blanks it’d work); which means if you’d put the code alone it would not work.
You are meant to write your own code based of the pseudo-code.