GAMES DEVELOPMENT // TASK 03

 Hasnol Rafiq Bin Hasnol Raduan / 0356767

Games Development / Bachelor of Design (Hons) in Creative Media / Taylor's Design School



Task 03:

Unity is a powerful and versatile game development engine that provides numerous tools and features that simplify the creation of a 2D platformer.

Unity Setup

Unity has robust support for sprites, including sprite atlases, sprite sheet slicing, and the Sprite Renderer component for displaying 2D graphics.  Animator and Animation tools enable the creation and management of complex animations for characters, enemies, and other game elements. 

Unity includes a dedicated 2D physics engine with components such as Rigidbody2D, Collider2D (e.g., BoxCollider2D, CircleCollider2D), and various joint types. These tools are essential for implementing realistic movement, jumping, and collision detection in a platformer. Easy-to-use collision detection and trigger systems allow developers to define interactions between the player, enemies, and other game objects.

Unity uses C# as its primary scripting language, which is powerful and versatile. The scripting API provides extensive control over game behavior, physics, animations, and more. Unity's event system simplifies the process of detecting and responding to player inputs, collisions, and other game events.


Player Script

Player Movement:

The provided code defines a `PlayerMovement` class in Unity that manages the movement, jumping, and attacking behaviors of a player character in a 2D platformer game. The class uses public fields for configuring movement speed (`moveSpeed`), jump force (`jumpForce`), a ground check reference (`groundCheck`), and a ground layer mask (`groundLayer`). In the `Start` method, it initializes references to the player's `Rigidbody2D`, `Animator`, and a `PlayerAttack` component, and freezes the player's rotation to prevent unwanted rotation during movement. 

The `Update` method continuously checks if the player is grounded using `Physics2D.OverlapCircle` and updates the player's horizontal movement based on user input, flipping the sprite's scale to face the movement direction. If the player is grounded and the jump button is pressed, a vertical velocity is applied to make the player jump. The method also updates animator parameters to reflect running and jumping states. When the attack button is pressed, it triggers the attack animation and calls the `Attack` method from the `PlayerAttack` component. The `OnDrawGizmos` method provides a visual representation of the ground check area in the Unity editor for debugging purposes.

Player Attack:

The `PlayerAttack` class in Unity manages the attacking behavior of the player character in a 2D platformer game. It defines public fields for configuring the attack range (`attackRange`), the enemy layer mask (`enemyLayers`), and the attack damage (`attackDamage`). The `Attack` method calculates the attack position based on the player's current position and direction, and then uses `Physics2D.OverlapCircleAll` to detect any enemies within the attack range. 

The detected enemies are logged for debugging purposes. Each enemy hit by the attack has its `TakeDamage` method called, applying the specified damage. The `OnDrawGizmosSelected` method provides a visual representation of the attack range in the Unity editor, allowing developers to see the attack area when the player object is selected. This is done by drawing a red wire sphere at the calculated attack position using the Gizmos API.


Mob Behavior Script

The `MobBehavior` class defines the movement and interaction logic for an enemy character in a 2D platformer game. The enemy patrols between two points (`pointA` and `pointB`) and can chase the player if they come within a specified detection range (`detectionRange`). The enemy's movement speed is controlled by the `speed` variable, and its health is managed by the `health` variable. The `Start` method initializes the enemy's `Rigidbody2D` and `Animator` components.

 In the `Update` method, if the enemy is not stopped (`isStopped`), it checks the distance to the player and either chases the player or patrols between the two points. The `Patrol` method handles the enemy's movement between the patrol points, flipping the enemy's sprite to face the direction of movement. When the enemy takes damage, the `TakeDamage` method reduces its health and stops its movement if health remains or destroys the game object if health is zero. The `OnTriggerEnter2D` method triggers the enemy's death when it collides with the player. The `OnDrawGizmos` method provides a visual representation of the patrol points and detection range in the Unity editor for debugging purposes.

Comments

Popular Posts