Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Question Physics Air Hockey.

Discussion in '2D' started by Alexey045, May 13, 2023.

  1. Alexey045


    Jun 4, 2022
    Hello everyone.

    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.

    What is the best way to do this?
  2. Kurt-Dekker


    Mar 16, 2013
    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

    Main control script is


    proximity_buttons is presently hosted at these locations:
    Last edited: May 15, 2023
  3. MelvMay


    Unity Technologies

    May 24, 2013
    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.
    Alexey045 and Kurt-Dekker like this.
  4. Alexey045


    Jun 4, 2022
    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)
  5. Kurt-Dekker


    Mar 16, 2013
    I have no idea what you mean... please don't tack onto your old post with new stuff.

    Instead, start a new post... it's FREE!

    When you post, keep this in mind:

    How to report your problem productively in the Unity3D forums:

    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.

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags:
    Alexey045 likes this.