August 15, 2019
Unity Pokemon Tutorial
Random Encounters
Youtube – Lets Make… Pokemon in Unity! – Episode 4 Encountering Pokemon in Long Grass
By: BrainStorm Games
This tutorial sets up the random encounter mechanic. This starts with the script LongGrass, that deals with 2D colliders to allow for a probability check when the player collides with certain tiles to see if they encounter a random battle and what is encountered.
The setup places a 2D box collider on each LongGrass tile that is a trigger. The player character has a 2D box collider (not as trigger) and a RigidBody 2D component. They just set the gravity scale of the RigidBody 2D to zero and that was it. These seems like an improper time to use a RigidBody 2D component as we’re just using it to detect a trigger collision.
I also tried modifying some values in the RigidBody 2D so it fit the situation better, but none of them interacted with the LongGrass colliders properly. I tried making the Body Type “Kinematic” instead of “Dynamic”, as well as checking off “Simulated”, but neither of these collided with the LongGrass triggers at all. In testing this, I placed a Debug.Log in the OnTriggerEnter2D encounter method just to make sure it was detecting the collisions, and I noticed a bug where moving left or right in the LongGrass was actually calling for an encounter chance twice for some reason. This also did not apply when moving up or down, just for left and right motion.
The way they switch from the overworld game to the battle scene also seems improper. They aren’t seperate scenes, they are just two different cameras placed in different locations in the scene space. The transition from the overworld to an encounter just turns off the overworld camera and turns on the battle camera. This seems sloppy and cluttered as you then have everything running in a single scene at all times.
Another note I did not like was that the LongGrass gets the GameManager component by tag. They created a “GameManager” tag for the GameManager and that is what they use to reference it and grab its script from other scripts. I am becoming less of a fan of tags as I work with various Unity projects as they are annoying to keep track of and finicky in code since they require typing an exact string value in. This can be lessened by creating a string variable to hold the tag name in your script so at least you only need to type it out properly once for that script, but it still seems to be a pain to work with and error prone, not to mention difficult to modify.