UNITY | PROJECT | SPACE SHOOTER 2D | 2. Player Input Movement

SPACE SHOOTER 2D

2. Player Input Movement

2
Player Control Script
  • Creating & Adding “PlayerControl” script

3
Player Control Script | Update() | Input.GetAxisRaw()
  • setting “speed” variable as public to see in the inspector
  • In “Update()”
    • set horizontal & vertical input by using Input.GetAxisRaw()
      • with Input.GetAxisRaw(), the values will be -1, 0 or 1
    • with the two inputs, make “direction”
      • by adding “.normalize”, make a unit vector
    • add a customized fuction “Move()” to move “Player”
      • we need this fuction in “Update()” because “Player”‘s position must be changed and detected every frame

 

4
Player Control script | Move() | Vector2 | Camera.main.ViewportToWorldPoint()

 

5
Player Control script | Move() | transform.position | Mathf.Clamp()
  • get the Player’s current position with “transform.position”
    • transform.position of the gameObject this script is attached to
  • calculate the new position in “currentPosition” variable
    • with “speed” variable, we can change the speed of “Player”
      • “direction” is the normalized vector2
      • “Time.deltaTime” is very important with this, we smooth the movement
2. Player Input Movement
without “Time.deltaTime”, the value(“direction”) is almost (0, 0), (1, 0)……

 

6
Setting Player’s Speed | Public Variable

 

1
Player Input Movement
2
Player Input Movement | without Time.deltatime
  • without “Time.deltaTime”, “Player” moves too dynamically