I know to use collison and phisics but I cant to do a cool impact reaction like that arms in a pinball game.
How do I do these vanes ? if I do a fast move with them the ball cross the object .
looks like that fast objects dont react very well in physics.
Hi, welcome to the forum!
As you say, something as fast as a pinball flipper is difficult to simulate with collisions, but you can cheat a little bit…
Start off by getting the position of the ball and the position of the flipper’s pivot. You can tell if the ball is within range of the flipper simply by finding the distance between these two points with Vector3.Distance. Next, you need to find the direction of the force the flipper will apply to the ball. For the right hand flipper, subtract the ball’s position from the pivot’s to get a vector:-
var ballPivotDir: Vector3 = ball.position - flipper.position;
Then, get the direction normal to the playing surface (it won’t be directly up, because the surface will be tilted). Get the cross product of the ball direction vector and the surface normal:-
var strikeDir: Vector3 = Vector3.Cross(ballPivotDir, surfNormal);
The resulting vector is an approximation of the direction in which the ball will be struck. Normalise this vector and multiply it by whatever amount of strength is appropriate for the flipper:-
strikeDir.Normalize();
strikeDir *= flipperStrength;
Then, use this vector as the parameter to rigidbody.AddForce on the ball. However, try using ForceMode.Impulse for the AddForce call (an impulse basically means a “knock”):-
ball.rigidbody.AddForce(strikeDir, ForceMode.Impulse);
For the left flipper, the calculation is the same but the direction is reversed - you can include this in the flipper strength multiplication:-
strikeDir *= -flipperStrength;
As you apply the impulse, move the flipper very quickly, or immediately move it to the extreme position, and it should look as though the ball has been given a solid whack.
Ok , my doubt now is…
if I M not using a real collision detection, how can I get the surface normal “surfNormal” ??
Use real collision detection, but just don’t rely on it working for the flipper. If the surface is flat, then the normal is the same at each point, and you can probably get it from the object’s transform. (For example, if the top of the object forms the playing surface, you might be able to use transform.up for the normal.)
it’s useful…