Blog01-Twin-Stick Shooter: Top-down 2D Character Controller
The first prototype game of three to be made within the ‘Unity’ game engine this trimester would be a ‘top-down twin-stick shooter’. My main goal for the first two weeks was to create the 2D character controller itself so that the movement of the character could be controlled with the keyboard and the mouse could be used to aim a weapon. I also intended on setting up the player’s health so that when the character comes into contact with an enemy, they lose points and to set up a health bar which would reflect how many health points the player had left.
There are multiple ways that a game object - such as a game character - can be moved around the game world. This video below shows the different ways it can be done within Unity and the differences between them:
When I spoke to one of my lecturers, he reminded me that ‘Transform.Translate’ (or using ‘Transform.Position’) doesn’t actually ‘move’ a game object, but ‘teleports’ it to new coordinates. This is a quick, easy way to make a game object appear as though its moving, but because it isn’t really, physics aren’t applied to it when it’s first acted on. Doing things this way can cause odd situations like game objects appearing to ‘move’ into or through other game objects because they didn’t technically come into contact with one another when one of them changed locations. This way is only useful when one knows that the game object they will be ‘moving’ doesn’t need to collide with anything or be affected by gravity.
‘Rigidbody.MovePosition’ is similar to ‘Transform.Position’ in the sense that one is simply giving the object new coordinates - It doesn’t add velocity or change the velocity. However, it does recognise and interact with colliders between its original position and new position per fixed update as long as the rigidbody isn’t set to kinematic (which means it doesn’t use physics at all).
He also said that ‘Rigidbody.AddForce’ is considered to be the most realistic way of moving a game object, however it’s more complicated and requires more tweaking to get the desired movement.
‘Rigidbody.Velocity’ allows one to move a game object with physics, however, isn’t as realistic as it just simply changes the velocity directly. This is a good way of actually moving something with physics, and at the same time, make it easier to control how an object moves. This is the way I decided to have the player control the character.
I then looked up how to get the weapon to aim in the direction of the mouse and fire using the left mouse button:
For some reason, despite following along with the tutorial, the projectiles were being spawned with the wrong rotation as well as would travel in random directions that didn’t link up with where the weapon was pointing at the time.
I had asked for help, and it turns out that the projectile script had been written in a way that meant that the calculation had a double up. ‘Transform.Translate’ - by default - uses a game object’s local transform information, however, what was needed was for the projectile to move based off of the world space’s coordinates. So, therefore, ‘Transform.right’ (also local) needed to be replaced with ‘Vector2.right’ (global).
I am concerned that moving the weapon with ‘transform.rotation’ may cause some issues in future. I’ve been trying to keep the character controller physics-based, however, setting the game object’s transform rotation directly (like in the tutorial) isn’t consistent with this goal. I may look into another way of doing this later in the project (e.g.: using Hinge Joint 2D). I will next look into setting up the player’s health and health bar as I’d planned.
References:
Blackthornprod (2018). HOW TO MAKE A 2D RANGED COMBAT SYSTEM - UNITY TUTORIAL [YouTube Tut], Retrieved February 8, 2019, from: https://www.youtube.com/watch?v=bY4Hr2x05p8
CodeJunkies (2018). Unity 2D - 8 Direction Player movement using Physics [YouTube Tut], Retrieved February 8, 2019, from: https://www.youtube.com/watch?v=km-04aUJy4o&t=99s
Unity3d College (2017). Unity 101: 5 ways to move Unity3D Objects [YouTube Tut]. Retrieved February 8, 2019, from: https://www.youtube.com/watch?v=tNtOcDryKv4&t=194s
<--BACK
















