I have a problem with my physics system, the ball only bounces off one wall, while sticking to the other, and im not sure how to fix it.
First wall:
Second wall:
Here’s the part of the script that does the wall-bouncing:
local rayDir = vel.Unit*Ball.Size.Y/2
local movementRay = workspace:Raycast(pos,rayDir,params)
if movementRay then
local normal =movementRay.Normal
if normal == Vector3.new(0,1,0) then
vel = Vector3.new(vel.X,-vel.Y*BounceAmount,vel.Z)
else
vel *= -normal
end
--func:PlaySound(SoundService.SFX.BallGround,.5,Ball)
end
Im assuming you mean the second time it bounces by “second wall”. I’m on mobile and can’t test anything, but would resetting each variable work? Also, could we see the code that detects the wall touch?
Is it just that second wall, or is it every wall except for the one that works? If it is just the first wall that works, then your variables might not be in relation to the wall’s cframe, in which case you might want to use cframe.lookvector. Sorry if this wrong or sounds stupid, but this is the best answer I can give lol
The problem might be with your ray cast params. Try debugging using print after your if statement regarding your normal. Also, don’t assume I am correct in this, I’m a beginner scripter and can’t test any code right now, so I am offering possible causes for the problem.
The issue is that the formula you’ve used does not actually result in a reflected vector. We need to use the dot product (not the Hadamard product) of the ball’s velocity and the surface’s normal in order to properly reflect the velocity vector.