I’m trying to make Air Hockey and have a problem with physics for players and a ball.
How to make the ball not to affect on players by ball’s forces? But players should collide with static colliders.
It’s an interesting problem because of combining direct control of the paddle with indirect control of the ballistic ball.
I made a quick attempt over in my proximity_buttons package and it mostly works.
My approach was direct continuous drive of the paddle to wherever you last touched. It uses Rigidbody2D physics and continuous collision detection.
The one remaining issue is the way the paddles “pull back” from the edge colliders. This is due to them receiving bounce velocity and moving for one full frame. If I remove the bounce then they don’t push away like that. But then the paddles don’t really smack the ball.
I tried zeroing the velocity in an OnCollision call but it appears the movement has already taken place, as confirmed by the execution timing diagram graphic in the Unity docs.
I’d be delighted if @MelvMay looked at it and had any insight into the bounceback problem. I think proxy non-bouncy paddle / walls will be necessary, layered so they don’t hit anything except themselves, then used to drive the actual paddle.
ANYWAY… this fully works, fully-playable, give it a shot.
It should also already work out-of-box for iOS / Android touch screens with two fingers.
Look for DemoAirHockey
Main control script is AirHockey2D:
proximity_buttons is presently hosted at these locations:
In 2023.1 onwards you can select send/receive forces per collider per layer:
Using the above you’d set receive layers to not include the ball so you would send a force to it and it’d have a collision response but you wouldn’t receive a force. Alternately you do this on a ball where it’d not send but still receive.
In earlier versions though you’ll need to make the paddles Kinematic so they don’t receive forces. This means though that you’ll not get a collision response from anything such as the walls so you’ll need to use physics queries to detect those and resolve them. You can also turn on useFullKinematicContacts and get callbacks for your paddle and resolve any overlaps.
It’d be easier to just restrict the paddles to the rectangular region inside the walls so you only have to resolve an overlap with the other player which should be simple, especially for simple circles.
Sorry for the late reply. Thanks for your response! Also I have a question, if I want to make player which can interact with game objects (boxes) like a dynamic, but different with NPC (mb just like dark souls)
This is the bare minimum of information to report:
what you want
what you tried
what you expected to happen
what actually happened, log output, variable values, and especially any errors you see
links to documentation you used to cross-check your work (CRITICAL!!!)
Don’t TALK about code without posting it. Do NOT retype code. Copy/paste and post code properly. ONLY post the relevant code, and then refer to it in your discussion.