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. Dismiss Notice

Question What is the best way to implement simple 2D collisions (beginner in Unity)?

Discussion in '2D' started by Invisipac, Jul 12, 2023.

  1. Invisipac

    Invisipac

    Joined:
    Jun 27, 2023
    Posts:
    4
    I have just started learning 2D game development in Unity and as a starting point I am trying to make a simple pong game.

    I want to move my paddle up and down have it collide with the top and bottom of the screen. One way I have accomplished this so far is with a dynamic rigidbody. However, if the ball ever hits the top or bottom of the paddle it moves the paddle as a result of, I'm guessing, the physics simulation. I want to avoid this without any sort of bypass (i.e giving the paddle a very large or mass, or something like that).

    I have tried using the Character Controller component but the collisions did not work no matter what I did, though I may have missed something. I was also considering making the paddle a kinematic rigibody and writing code for the collisions myself but I feel like this would become difficult if I were to make a more complex game where there are a variety of collisions, unless there is some standard way of writing such code.

    I would appreciate any suggestions on what the proper way to implement this is.
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,342
    MelvMay likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    CharacterController is 3D physics so that's why it doesn't work with 2D physics.

    As above, but you don't need to calculate the collisions yourself. A Kinematic collider will still collide with a ball which would likely not be Kinematic but the default Dynamic type.

    A Kinematic body/collider won't have forces applied to it from collisions meaning it does have collisions. This is perfect for a paddle as you don't want the ball to cause it to move as it's kinematic movement under the players control. The ball isn't Kinematic, it's dynamic and will bounce around and hit the paddle.
     
  4. Invisipac

    Invisipac

    Joined:
    Jun 27, 2023
    Posts:
    4
    Thank you so much for the replies. I apologize for not making it clearer in the question but I was more confused about how to make the paddle collide with the boundaries of the screen since the kinematic rigidbody does not stop when it hits the rectangles that I use for the top and bottom bounds.

    I seem to have found a solution that works for me, though again I do not know if this is the most optimal. I made the paddle kinematic as you all recommended but I also added a call to the Rigidbody.Cast function to check for collision with the boundaries as I saw someone do in a tutorial for a top-down 2D RPG.

    Thank you again for the help, and I would appreciate any feedback if using raycasts is the way to go or if their is a better way. Eventually, I would like to try and make an RPG and so I am looking for a generalizable method that I can use in the future.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Well it's Kinematic motion so yu can also just limit where you ask it to; you don't need the physics system to tell you that. Have up/down position limits and never use MovePosition to move beyond those. Use two floats and a gizmo to draw the limits to help you edit it or just two GameObjects with their icons on to show it and use their Transform positions as the limits.
     
  6. Invisipac

    Invisipac

    Joined:
    Jun 27, 2023
    Posts:
    4
    Thank you again for the advice.

    I added those limits with the help of the objects' transforms and their box collider extents and it works. However, I hardcoded these values and I was hoping there would be a more general way of determining these bounds. Should I add a reference to the game objects that represent the top and bottom inside my MovePaddle script or does that introduce uncessary coupling?

    Additionaly, if my future goal is to try and make a top-down RPG what would be my approach there, especially since I would not be able to hardcode the bounds for every single obstacle.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Yes, just reference their Transform. For such a simple thing, I wouldn't worry about "coupling" or any other design consideration. Casting the paddle Rigidbody2D and its colliders in the hope to use that info for limits is still coupling just of a different form. Use whatever is the easiest for you.

    Completely different problem but top-down RPG doesn't describe a specific problem. Too many variations on movement, AI etc to say, do it "this" way. A paddle in a breakout game is far from a NPC with lots of movement variation and decision making to do. :)
     
  8. Invisipac

    Invisipac

    Joined:
    Jun 27, 2023
    Posts:
    4
    Ok I think I understand. Thank you so much for all the replies it was extremely helpful and informative.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    MelvMay likes this.