top of page
Search
Writer's pictureNg Wei Shun

Zombie Shooter DevLog #1 : Basic Features

Welcome to another DevLog for Zombie Shooter. Today I will talk about code used for movement and camera controls I used for the game, and also some code involving creating a custom cursor and rotating the player towards the cursor.


Movement

When coding for an object’s movement in Unity, there was plenty of ways to do so. For my game, I chose to add velocity to the character’s RigidBody component(the component of the object that handles its physics).

There were in-built settings in Unity that recognized that Left and Right arrow keys and A and D keys to move horizontally, and the same for Up and Down arrow keys and W and S keys to move vertically. This allows me use these values to calculate movement, instead of checking if any movement keys were pressed.


movement.x = Input.GetAxisRaw("Horizontal");

movement.y = Input.GetAxisRaw("Vertical");

rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime);

Camera controls

For a top-down shooter, I need the camera to follow the player around the map. I made this using Cinemachine, a plug-in that made camera controls very simple.

All I had to do was specify the object that the camera had to follow. Then I could customize the camera to move slightly further when moving so that the player can see what’s coming, and have a small area in the center where the camera would not move if the player was within the area.

Rotating the player

In order to make the game more realistic, I wanted to make the player character rotate towards where they were aiming. This requires the direction between the player and the cursor to be calculated every frame.

To do that, first I have to convert the player’s coordinates from World Space to Screen Space. This is because I’m comparing the player’s and cursor’s position relative to the screen. (If they were both on world space, the player would face wherever the cursor is relative to the whole scene)


Vector3 mousePos = Input.mousePosition;

Vector3 objectPos = Camera.main.WorldToScreenPoint(rb.position);


After calculating the difference between the player’s and the cursor’s coordinates, I can find the angle between the two coordinates using the Atan2 function, similar to what I used in Space Defenders. It basically calculates the angle of a given line from the x-axis.


angle = Mathf.Atan2(mousePos.y - objectPos.y, mousePos.x - objectPos.x) * Mathf.Rad2Deg;

I then set the RigidBody’s rotation to the angle.

rb.rotation = angle;


An alternative way was to rotate the object’s rotation instead. However, I have heard of warnings of avoiding tweaking with object’s transform component when there was a RigidBody, although I have not figured out exactly why yet.


And with that, I can now move around the map and look around easily. This makes it easy to visualize and test new gameplay features moving forward.




That’s all for today, and expect more interesting features to come!

3 views0 comments

Recent Posts

See All

Bình luận


bottom of page