Search Unity

Question Physics Air Hockey.

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

  1. Alexey045

    Alexey045

    Joined:
    Jun 4, 2022
    Posts:
    6
    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

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    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
    :

    https://github.com/kurtdekker/proxi...s/Assets/DemoAirHockey/Scripts/AirHockey2D.cs

    Screenshot-MyGame-133285535102753220.png

    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/
     
    Last edited: May 15, 2023
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    In 2023.1 onwards you can select send/receive forces per collider per layer:

    https://docs.unity3d.com/2023.1/Documentation/ScriptReference/Collider2D-forceSendLayers.html
    https://docs.unity3d.com/2023.1/Documentation/ScriptReference/Collider2D-forceReceiveLayers.html

    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

    Alexey045

    Joined:
    Jun 4, 2022
    Posts:
    6
    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

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    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:

    http://plbm.com/?p=220

    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: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    Alexey045 likes this.