As a software engineer, I've always been fascinated by the intricate world of game development. Despite my experience in coding, stepping into this realm felt like opening a door to an entirely new universe. This blog aims to share my journey as a beginner in Unity game development, along with some initial tips and tricks I've picked up along the way.
Why would I make a video game when I can just play them?
I have been playing video games as long as I can remember. I have always used them as a release from stress and anxiety, but its also how I met some of my best friends. Countless nights of staying up late on a variety of games, weekends of competing in fps (first person shooter) tournaments, and even hosting LAN parties at my house.
How does Unity handle player movement?
Unity offers various methods for handling movement inputs, including Transform, Rigidbody, and CharacterController. For my project, I am utilizing Rigidbody. Below are some examples of Rigidbody methods essential for the PlayerMovement script:
Update() Handles input for movement (WASD keys) and call the RotateTowardsMouse method to rotate the player towards the mouse position.
void Update()
{
movement.x = Input.GetAxis('Horizontal');
movement.y = Input.GetAxis('Vertical');
RotateTowardsMouse();
}
FixedUpdate() Moves the player based on the movement vector and speed.
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
RotateCharacterTowardsMouse() Calculates the angle between the player and the mouse position, then rotates the player to face the mouse.
void RotateCharacterTowardsMouse()
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (Vector2)(mousePosition - transform.position);
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
rb.rotation = angle;
}
Most components created for the project will require scripts for customization. Note that Unity will automatically update the script as you make changes through the UI.
Once you have created the PlayerMovement script, you can assign it to the player GameObject by dragging and dropping it from the Project window into the Inspector window. Alternatively, you can click the Add Component button in the Inspector window, search for PlayerMovement, and select it.
After configuring the movement, you will be able to move an empty square model around.
Lets add some custom sprites!
Setting up the player and zombie sprites is a fun process that I enjoyed. My friend Joey created the sprites and I was able to build all the surrounding pieces to make them work. I imported the custom sprites for player models, zombies, and scenery into their folder, plugged them in, and worked on setting up idle/running states.
References:
SOCIAL SHARE CARD GENERATOR