Top Down Shooting Game (part 2)

Today I drew a picture of my soldier and change my player to it. And also make player have to rotate and follow the mouse to shoot enemies more easily. Changed its movement from arrow keys to W A S D to make it easier to play.

Soldier Sprite Sheet

First is import the solider sprite sheet in Assets. Then change its Sprite Mode into Multiple and click apply. And go to Sprite Editor, change the type to Grid By Cell Count in slice. Then make its column is 4 (because I drew 4 soldiers in a sheets, if you draw 6 then make its column to 6) then click apply to save.

Movement

In PlayerMovement script, create a pos with Vector2 variable type and equal to transform.position. Transform is a component that controls the position, rotation, and scale of a GameObject in a scene. Use If-else statement to check If the key down is “w” then pos.y is plus moveSpeed and if the key down is “s”then pos.y minus the moveSpeed. And do the same with “a” and “d”. Then the transform.position equal pos.

Player rotate

Create a camera gameObject called cam and a mousePos with Vector2 variable type. In the Update() function, mousePos equals the cam.ScreenToWorldPoint(Input.mousePosition). This command will return the mouse position.

In the FixedUpdate() function, create a lookDir equal mousePos – rb.position. Create an angle is Mathf.Atan2(lookDir.y, lookDir.x) * Mathd.Rad2Deg – 90f. And rb.rotate is equal to the angle.

Now the player can rotate and its movement changes to W A S D to be easier to use.

Tags:

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *