Intro to Unity DOTS Physics
Full workflows and code on how to get started with Unity DOTS Physics

What you'll develop in this DOTS Physics section

Shooting red bullets to destroy asteroids, powered by DOTS Physics

Functionalities included

  • Unity DOTS Physics
    • Using FixedStepSimulationGroup for custom systems
  • Physics Body
    • Adjusting PhysicsVelocity programmatically
  • Physics Shape
    • Updating shape type to prefab
  • Physics Categories
    • Custom categories for collisions and interactions
  • Triggers on collisions
    • Changing stateless triggers to buffer of stateful triggers
  • Acting on triggers to change materials and destroy entities

Unity DOTS Physics

Unity Resources

Unity's 2019 overview of physics in DOTS: https://www.youtube.com/watch?v=tI9QfqQ9ATA Recommended to watch.
Unity's 2020 update of physics: https://www.youtube.com/watch?v=n_5RGdF7Doo If interested in the latest and greatest.
Link to Unity's DOTS Physics samples: https://github.com/Unity-Technologies/EntityComponentSystemSamples/blob/master/UnityPhysicsSamples/Documentation/samples.md Good place to see the contents of the Physics Samples without opening Unity Editor (ReadMe is missing some samples that are contained in the repo so a little misleading)
Unity's DOTS Physics v0.6.0-preview.3 documentation overview: https://docs.unity3d.com/Packages/[email protected]/manual/getting_started.html. Key to get an understanding
Unity DOTS Physics forum: https://forum.unity.com/forums/dots-physics.422/ Helpful to find answers to questions.
πŸ”‘ Major Key Alert πŸ”‘
A good way to approach implementing DOTS Physics is by checking out the DOTS Physics samples and finding an example similar to what you're looking to implement, and then copying, pasting, and modifying to your needs. This is the approach we will use in this section.
The current Physics documentation is difficult and trying to build these functionalities from scratch is a Sisyphean task. It's much simpler to grab code from Unity's samples since there's a lot of "boilerplate" for a majority of the Physics interactions.
Checkout how Code Monkey follows this technique in his "Getting Started with Unity DOTS Physics" video: https://www.youtube.com/watch?v=B3SFWm9gkL8​

Setting up for this gitbook section on DOTS Physics

  • Go to https://github.com/Unity-Technologies/EntityComponentSystemSamples and download the repository for Unity's ECS Samples (if you haven't already)
    • Or you could use:
    git clone https://github.com/Unity-Technologies/EntityComponentSystemSamples.git
    • in terminal to download the repository
  • Open the downloaded repository navigate to UnityPhysicsSamples > Assets > Demos > Setup > Events > 2d1. Events - Triggers.unity
    • This will warn you that our Unity version (2020.1.17f) is different than the sample version
    • This is okay and will not cause issues so click Continue
    • Set the Unity version to 2020.1.17f and open the project and navigate to 2d1. Events - Triggers and hit play
Download Unity ECS Samples repo
Showing Unity Physics sample
  • This is a good sample because it shows both triggers and collisions
    • Breaking the translucent red cubes triggers gravity to switch
    • The balls collide with each other
  • Navigate into the "2d1. Triggers" folder and open "2d1a. Triggers - Change Materials"
Navigating to Change Material scene
  • We will base our Physics updates to our project on this sample
  • The balls change colors when passing through the colored prisms and collide with the block objects
  • Our bullets will pass through and change the material of asteroids and players (similar to how the balls pass through the prisms)
  • Bullets passing through an asteroid or player will also trigger adding a DestroyTag to those entities which will cause them to be destroyed
  • Select different prefab GameObjects in the Hierarchy (ex: "Floor" under Physics Scene Basic Elements), and notice the Physics Shape component in the Inspector
    • Under "Collision Filter" in the Material section (still in the Physics Shape component) check out the "Belongs To" and "Collides With" fields
      • If you can't find Collision Filter, expand the Physics Shape component section
    • Different GameObjects in the Hierarchy have different values set for "Belongs To" and "Collides With"
    • This is how we tell Unity Physics what entities interact with each other
  • Scroll to the bottom of the "Collides With" drop down list and select "Edit Physics Category Names" to be taken to PhysicsCategoryNames in the main Assets folder
  • Take a look at the different categories for all the samples in the repo
Navigating through GameObjects in the scene Hierarchy and taking a look at Collision Filter fields
  • We can see that in DOTS Physics, the Physics Shape component on our prefabs will decide what the prefab interacts with
  • Also in the Physics Shape component under "Material" there is a field called "Collision Response" with 4 options:
    • Collide (bang together, which our players and asteroids will do)
    • Collide and Raise Collision Events (bang together and cause an event; we will not be using this in this gitbook only because we do not want to over-complicate things by doing too much)
    • Raise Trigger Events (not bang together but cause a trigger event, like our bullets will do to players and asteroids)
    • None (nothing happens from collisions)
To best prepare for the following DOTS Physics code-alongs, we recommend you complete the following check-list:
  • Watch a Physics explanation video(s)
  • Read through Unity's DOTS Physics overview documentation
  • Download and open Unity's Physics samples in the repo you just downloaded above
  • Navigate sample scene Hierarchies and check out the different Physics Body and Physics Shape settings to get a feel of different set ups