Powering your game with Physics!

Learn how to use 3D & 2D Physics in Unity

Pablo Gómez Platón
6 min readApr 7, 2021

Ah, Physics! We meet again…

Golf Journey: A 2D-Physics platformer game I made! It’s short (and a bit hard), give it a try! (Only for Android)

You have probably seen one of those videos where players play with Ragdolls, props, or any kind of physical object, mostly having fun with certain situations within the game using physics. If you are starting out, learning about the Physics engine is one of the easiest first things you can do!

In this article, I’m going to show you how to use those physical objects for any kind of purpose (except Ragdolls, which they are another different broad topic).

I will use this project to showcase the 2D physics

Knowing the basics

Before getting into Rigidbodies, there are a couple of things you must learn to further understand how collisions or physics work.

Colliders

For our GameObjects that we’ll want to physically interact, they will always need a collider. Unity provides you with primitive collider shapes (cube, sphere, capsule…) and other complex colliders (polygon, mesh…).

Every collider has a variety of variables which you can modify, such as the size of a collider, the offset of that collider, the physical material, and the “Is Trigger” option. More on the latter in my next article.

Simple Circle collider for the Golf ball!
3D Capsule collider

Update Vs FixedUpdate

In Unity (or in other game engine, really), there are two types of Updates, one for game logic that needs to be updated every frame (Update), and one for physic calculations (FixedUpdate).

Calculating physics and their interactions can be very expensive for the computer, and it would be even more expensive to perform those operations every frame. That’s where FixedUpdate solves that problem. As the name suggests, it will update the physics every quantity of time passed (Fixed Timestep) independently of the game’s frames.

Any operation that involves Rigidbodies should be handled in FixedUpdate (moving, rotating, adjusting speed, applying forces…)

The default Timestep value is set to 0.02 seconds. You can always change it to a higher or lower value in Project Settings > Time > Fixed Timestep.
I recommend you not to touch it unless you know what you’re doing.

Filtering Collision Layers

To improve performance, or overall, avoiding collision between the same or different layers, we can use the Layer Collision Matrix. It will determine which layers can interact with eachother (for example, some games use this to avoid player-to-player collisions or “body block”).

You can access this Matrix by going to Project Settings > Physics or Physics 2D.

Remember to change your GameObject layers!

Rigidbodies

The component that truly adds physics to our GameObjects. It has all the properties you may expect from a physical object (plus other game related properties).

While 2D and 3D Rigidbodies have some small differences (aside that one discards the Z axis), I am only going to review the 2D Rigidbody’s properties. Here we go!

  • Body Type: There are three types (or two for 3D Rigidbodies):
    - Dynamic: Uses every feature of a Rigidbody. It will be affected by forces and gravity, and can be moved by other Rigidbodies.

    - Kinematic: Less features, but gives more control of the Rigidbody behaviour to the developers. In contrast to the Dynamic type, forces and gravity have no effect, and can’t be moved by other Rigidbodies.

    - Static: It doesn’t have any of the Rigidbody properties. You’ll be better off not adding a Rigidbody (only add a collider and that’s it). However, if you want it to be static and access the little remaining functionality of the Rigidbody for some reason, then, this option is useful.
  • (Physic) Material: Defines properties of the Rigidbody or a Collider, such as Friction or Bounciness.
The ball’s physic material
You can assign other Physic Materials to other colliders to create awesome effects (ice tiles with no friction, for example)
  • Mass: How much does the body weigh. Heavier objects will require more force power for them to be moved or launched. Oh, and remember that all objects fall equally, independently of their mass.
Trying to launch with the same force two balls of varying weight
  • Simulated: Enables or disables the physics simulation at run-time.
  • Auto Mass: Unity decides for you how a Rigidbody weighs depending on the size of its collider.
  • Linear Drag: How much is the Rigidbody’s positional movement slowed down.
Linear Drag
  • Angular Drag: How much is the Rigidbody’s rotational movement slowed down.
Angular Drag
  • Gravity Scale: It’s just a multiplier to the global gravity force. You can always change the gravity settings in Project Settings > Physics or Physics 2D.
  • Collision Detection: Modes that change the Rigidbody’s way to interact with other colliders. Since the 3D Rigidbody has two more options than the 2D, I will list those in here.
    - Discrete. Some overlap between colliders will occur as collisions are only calculated during a physics update.

    - Continuous. In 2D, overlaps are avoided as collisions determine the new position of the Rigidbody. In 3D, it’s a hybrid between Discrete (for Dynamic bodies) and the 2D continuous mode (for static bodies or colliders). Beware, this mode is CPU intensive.

    - Continuous Dynamic. Every collision against a collider, static or dynamic, are calculated extensively. Even more intensive.

    - Continuous Speculative. Collisions are predicted. Kinematic bodies are only allowed to use this mode of Continuous Collision Detection.
2D options. Only counts with the Discrete and Continuous mode
All options available in the 3D Rigidbody
  • Sleep Mode: Handles when should a Rigidbody go inactive or “go to sleep” to save processing power. The modes are self-explanatory, so, there’s no need for me to describe them to you (you’ll see for yourself).
  • Interpolate: Sometimes, Rigidbodies can have some “jittery” visuals or interactions.
    - None. Set by default. No smoothing is applied.

    - Interpolate. Movement is smoothed based on the GameObject’s positions in previous frames.

    - Extrapolate. Movement is smoothed based on an estimate of its position in the next frame.
Leave it at None if you don’t experience jittery movement
  • Constraints: Defines movement or rotation restrictions.
If you freeze the movement in the Y axis, the body won’t move up or down…
…like this…

Conclusion

This was a lengthy article. I hope you didn’t fall asleep while reading it. If you didn’t, you are ready to play with physics with a *basic* understanding of it. Thank you for reading my article!

The next article will be a lot shorter, and it will focus on the difference between a Non-Trigger and a Trigger Collider! If you liked this article, and want to see the next ones easily, follow me on Medium.

--

--

No responses yet