Make a bouncy ball

Hi chat, i’m currently trying to do a bouncy ball, when a Player will touch the ball, the player is gonna fling in the opposite direction of where the Player came from, here’s a image if you don’t understand


Thank you if you guys can help me!

4 Likes

You would need body position, and check the orientation of the players head

2 Likes

You can inverse the unit of the difference of the PrimaryPart Position and the Ball Position to get the opposite direction they are facing.

ball.Touched:Connect(function(hit)
    -- figure out if the ball was touched by a player then grab their character here
    local flingDir = (character.PrimaryPart.Position - ball.Position).Unit * -1 -- get the fling direction
    local force = 500 -- i recommend using the characters mass then multiplying that to get a constant force across all players, this is just an example
    character.PrimaryPart:ApplyImpulse(flingDir * force) -- ApplyImpulse may have some issues when it comes to characters, alternatively you can use a BodyForce and remove it after a single frame
end) 
3 Likes

Right, but isn’t the ball supposed to fling the character in the opposite direction from which they came? Character orientation shouldn’t matter here.

1 Like

Sorry, I assumed the player would always walk forwards. I will update my OP to account for this.

1 Like

Yes, it’s kinda hard doing that thing, but i appreciate you help!

A strong LineForce that only exists for a few frames should work.

As always you can do this in two more ways.

You could just set the elasticity of the ball to 1 and the elasticity weight of the players limbs to 0. Of course the outcome could vary and not be consistent but it is by far the easiest way.

The next recommended way is simply to reflect the players velocity. There is a bit of maths you need to know here.

R = V - 2N(V•N)
Where R is Resulting Vector
V is initial vector and
N is normal of reflecting plane.

You can get the normal by getting the direction from the ball to the player. (You could raycast but I don’t recommend it) Since the surface area of the ball always point out we can do this

local N = (otherPart.Position - Ball.Position).Unit

And then you can simply do this

local V = otherPart.AssemblyLinearVelocity

Then

local R = V - (2 * N * V:Dot(N))

Simply set that to the Velocity.

otherPart.AssemblyRootPart.AssemblyLinearVelocity = R

The third and final recommend method has already been said here:

That would take some tweaking to get the effect just right however it is quite easy to implement.

However (if I’m not mistaken) the PrimaryPart or AssemblyRootPart should always be the Velocity to modify. (Just a small note)

3 Likes

Thanks for the effort you put in that message! But what is the otherPart supposed to be? the Hit or the HumanoidRootPart?
Thanks

I guess it was the HumanoidRootPart because it works :tada:
Thank you a lot!
Here’s a preview tho (it’s so laggy :triumph:)

If you want the player to purely get flung, make it so the ball’s assemblyvelocity (I think thats what its called)
is adjusted to whatever flings the person away from the ball. (e.g the z value is changed to fling a player up)

1 Like

Glad it worked! I usually name the parameter otherPart instead of hit because for me hit doesn’t explain what is stored in the variable.

I tend to go down the route of readable code > writeable code most of the time. otherPart is also the default name in the documentation.

Out of curiosity which method did you end up using?

1 Like

I used a BodyVelocity which use as Velocity R * 25, made the Player Jump, then reduce the Velocity over time and finally delete the Velocity, it works well!