Skill Tree Activated

The quest to become an indie game developer

C#CodingGame DevScript-writingTrainingUnity

2D Game Development – World Interactions: Blocking Movement

Day 060 #100DaysOfCode
Day 014 #100DaysOfGameDev

In order to simulate movement and object collisions, I had to learn how to use the Unity physics system. This gets implemented via a Rigidbody component attached to a GameObject.

With a Rigidbody in place on a GameObject to govern how it should interact with the Physics System, I then had to define what part of those objects is the “solid” part by using a Collider.

Ruby’s Box Collider

Colliders are simple shapes mapped around objects to define the “edges” that will interact with other colliders as things start moving around

utilizing the Rigidbody and Box Collider components

Because my Character Controller script was written to use the Transform component to create movement, subtle conflicts with the new Collider components put in place would cause sprite “jitter” whenever a collision was detected. This was happening because the sprite would be moved via Transform, then a collision would be detected between two GameObjects and the Collision component would cause the moving objects to return to its previous position as the collision meant access was denied to that new position. These “fights” between the systems would result in the character looking like she was “jittering” while moving along the edges where collision boxes met.

To solve this problem, the movement script had to be re-written to instead check for collision first, then move if the movement was allowed. By using the Collider component to determine movement, this remedied the “jitter” situation and resulted in a clean stop when encountering another collider.

the updated movement controller script incorporating Rigidbody

After this, it was just a matter of going through the Prefabs to define proper collision boxes around any objects that could be interacted with and resize them so Ruby could pass behind and around them, but not through.

With this problem solved, all that remained was to learn how to apply this principle to the Tilemap so Ruby would be unable to walk on water any more.

applying a Composite Collider to the water tiles on the tilemap to make one big box

Tilemaps can be assigned their own colliders, so any tile that represented water was set up to block movement. Now the tilemap can be painted ad hoc and any new water tiles that get painted will automatically prevent movement onto them.

Leave a Reply

Your email address will not be published. Required fields are marked *