Creating a Metroidvania (like Hollow Knight) in Unity | Part 1: Movement and Camera

Terresquall
5 Apr 202322:40

Summary

TLDRIn this first episode of the Metroidvania tutorial series, viewers learn how to create a basic Unity 2D player controller inspired by Hollow Knight. The tutorial covers setting up a Unity 2D project, implementing player movement and jumping mechanics, and animating the character. It also demonstrates how to make the camera follow the player. By the end of the episode, you will have a player that walks, jumps with variable height, and is fully animated, with a camera smoothly tracking the character's movements. The next episode promises to expand on these mechanics with additional features.

Takeaways

  • 😀 Set up a Unity 2D project by selecting the 2D core template in Unity Hub and naming your project (e.g., 'Metroidvania Prototype').
  • 😀 Create basic player and floor game objects using Unity's 2D sprite options, ensuring proper collision with BoxCollider2D components.
  • 😀 Implement a simple walk mechanic by creating a PlayerController script and using RigidBody2D for player movement based on horizontal input.
  • 😀 Increase player speed and gravity through the Unity Inspector, allowing for smoother player movement and faster falling.
  • 😀 Add a jump mechanic that ensures the player can only jump when grounded, using raycasting to check if the player is on the ground.
  • 😀 Implement variable jump height, allowing players to control their jump height by releasing the jump button early.
  • 😀 Prevent player from sticking to platforms by creating and applying a physics material with zero friction to the player's RigidBody2D.
  • 😀 Animate the player character using Unity's Animator window, creating animations for idle, walking, and jumping states.
  • 😀 Set up transitions between animations in the Animator Controller, using parameters like 'walking' and 'jumping' to trigger these transitions.
  • 😀 Add a flip function to the PlayerController script, enabling the player character to face the direction of movement by adjusting its local scale.
  • 😀 Implement a CameraFollow script to make the camera follow the player character, with adjustable follow speed and offset for better visibility.

Q & A

  • What is the goal of the tutorial series?

    -The goal of the tutorial series is to help recreate the movement mechanics, melee mechanics, and spell-casting mechanics of the game Hollow Knight using Unity 2D.

  • Which Unity version is recommended for this tutorial?

    -The tutorial recommends using Unity version 2021.3.0 F1, as some features used may not exist in other versions.

  • How do you create a new Unity 2D project for this tutorial?

    -To create a new Unity 2D project, click the 'New Project' button in the Unity Hub, select the '2D Core' template, and then create the project. The recommended version is Unity 2021.3.0 F1.

  • What is the purpose of adding a BoxCollider2D to the player and floor objects?

    -The BoxCollider2D component allows the player and floor objects to collide with each other, ensuring the player can walk and jump on the floor.

  • What does the Rigidbody2D component do for the player?

    -The Rigidbody2D component gives the player weight, enabling them to interact with Unity's physics system, allowing gravity to affect the player.

  • How is the player's walking speed controlled in the script?

    -The player's walking speed is controlled by a 'walkSpeed' variable, which is set in the script and can be adjusted in the Unity Inspector. The player's movement is determined by the x-axis input and the walkSpeed value.

  • What is the purpose of adding the 'SerializeField' attribute to the walkSpeed variable?

    -Adding 'SerializeField' to the walkSpeed variable allows it to be edited in the Unity Inspector while keeping it private to the script, making it cleaner and more organized.

  • How does the script handle the player's jumping mechanics?

    -The script handles jumping by checking if the player is grounded using raycasting. If the player is on the ground and the jump button is pressed, the player's vertical velocity is set to the jump force, sending them upwards.

  • Why is a physics material 2D used in the player's Rigidbody?

    -A physics material 2D is used to set the player's friction to zero, preventing the player from sticking to walls or platforms when moving or jumping.

  • How does the player’s character flip based on movement direction?

    -The player flips by adjusting the local scale on the x-axis. If the player is moving left (xAxis < 0), the scale is set to -1 on the x-axis, and if moving right (xAxis > 0), the scale is set to 1, making the player face the correct direction.

  • What is the function of the CameraFollow script?

    -The CameraFollow script makes the camera follow the player by adjusting its position towards the player's position at a defined speed. An offset is used to ensure the player remains visible in the camera's field of view.

Outlines

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Mindmap

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Keywords

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Highlights

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Transcripts

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now
Rate This

5.0 / 5 (0 votes)

Related Tags
MetroidvaniaGame DevelopmentUnity 2DTutorialHollow KnightPlayer MovementAnimationsGame MechanicsIndie GameGame DesignUnity Tutorial