UNITY | PROJECT | RPG Tutorial |9. Weapon Animation

2
Creating Animation for Sword
316.png
Creating Animation for Sword
4
Creating Animation for Sword
  • create an Animation
    • by changing Transform(Position & Rotation) however I want
    • for each frame we can add changes
4-1
Creating Animation for Sword

 

5
“Animation” Component
  • Remove Animator(New)
  • Add Animation(Legacy)
    • Put the Animation for Sword in Animation Component as Animation & Element0
    • Since we have one animation, Set Size into 1
6
“Animation” | Legacy
  • in Animation Component,
    • in Debug tap, check Legacy
7
Animation | Wrap Mode | Once
  • to make Animation play once, change Wrap Mode into Once
8
InputManager | Key “Fire1” for Left Mouse Button
9
“SwingSword” Script
  • create Variables
    • for Sword we are yielding
    • for the status of sword
10-1
Animation Working
10
Adding Script to Sword
11
“AttackBlocker” Script
  • create a script to prevent Player from Yielding Sword in specific conditions
  • create variables one static and the other as path
  • in Update() method
    • put the static variable in the path variable
12
Adding “AttackBlocker” into “GlobalStat”
13
“Quest001” Script
  • add AttackBlocker.blockSword to Quest001 to change the status of swinging Sword
    • in OnMouseExit() method,
      • set the status as 0, indicating Swinging is available
    • in OnMouseOver() method,
      • when Action Button is pressed,
        • set the status as 2(actually 1), indicating Swinging is not available when grabbing the Notice
      • when Distance is less than 3,
        • set the status as 2(actually 1), indicating Swinging is not available when standing in front of the Notice Board
14
“SwingSword” Script
  • Add AttackBlocker.blockSword ==0 to the condition of if statement
14-1
Swinging Sword is not working

 

UNITY | PROJECT | 2D PlatFormer | 3. Basic Enemy

2

3
“PlayerControl” Script
  • Create a bool variable isGrounded as Flag
  • Create OnCollisionEnter2D() method,
    • when a collision with an object tagged “Ground”
      • the flag is set to “true
4
Tagging “Ground”
4-1
Collision Detection Working
5
“PlayerControl” Script
  • in Update() method,
    • add code as a condition for Player to jump
    • when the button is pressed and at the same time the flag is true(Player is grounded)
  • in Jump() method,
    • add code to set the flag to false
6
Box not tagged
7
Ground Tagged

 

9-1
Jump
  • Player is not jumping on Box because Box is not tagged as Ground

 

 

8
Creating “Enemy”
  • Create Enemy
    • add BoxCollider
    • add RigidBody2D
    • add EnemyControl Script

 

9
“EnemyControl” Script
  • Create variables for Enemy‘s speed and direction
    • Enemy is moving horizontally only, so take care of X
  • in Update() method
    • access velocity property to move the gameObject by using GetComponent
10
“Enemy” Inspector | Public Variables

 

11
“EnemyControl” Script
  • in Update() method,
    • add RayCasting to perceive the obstacles
    • Physics2D.RaycastHit2D(vector2 origin, vector2 direction)
      • the origin where Ray is cast from is the current position of gameObject
      • the direction of casting the ray is only x
      • when the length of ray is less than 0.7,
        • activate Flip()
    • create a function, Flip(),
      • change the direction in x opposite whenever this function is called
12
“Enemy” Inspector
  • put Enemy in Igonore Raycast layer
  • set the speed

 

12-1
Enemy Moving

 

13
When layer is defualt

 

14-1

UNITY | PROJECT | 2D PlatFormer | 1. Project Set Up

2
Importing Sprites

 

3
Cutting Sprites

 

4
Creating “Ground”
  • Add “Box Collider” to “Ground”

 

5
Adding “Player”
  • Add “Box Collider” to “Player”
  • Add “RigidBody2D” to “Player”
7.1
Adding “PlayerControl” Script
7.2
Physics Working

 

8
“PlayerControl” Script
  • Claim variable for Player movement
    • playerSpeed
    • facingRight as flag
    • playerJumpPower
    • movementX
  • in “Update()” method
    • get input to move horizontally by using “Input.GetAxis()”
    • get “RigidBody2D” component by using “GetComponent()”
      • call “velocity” propery to move “Player” with continuous input
    • create “if Statement” for Player’s movment
      • when pressing left and facing right
        • activate “FlipPlayer()” method
      • when pressing right and facing left
        • activate “FlipPlayer()” method
    • Create a Function, “FlipPlayer()”,
      • set the flag, “facingRight” to opposite
      • get the “localScale” property
        • multiply -1 to reverse the direction
      • Don’t forget to update “localScale”
10.2
Player Moving
9
“PlayerControl” Script
  • Create a function, “Jump()’,
    • use “AddForce()” to give force upwards to “Player”
  • in “PlayerMove()” method,
    • when “Jump” button is pressed, activate “Jump()” method

 

10.1
Input Manager

 

10.3
Player Jumping too high

 

11.1
RigidBody2D | Linear Drag | Gravity Scale
  • Set “Linear Drag” 0 to 1 to prevent “Player” from jumping too much
  • Set “Gravity Scale” 0 to 10 to prevent “Player” from jumping too much

 

11.2
Player Jumping
  • Player jumps normally but still it can jump multiple times in the air

UNITY | PROJECT | SPACE SHOOTER 2D | 11. Timer UI & GameTitle

2
Importing Timer UI

 

3
Adding Timer UI Image
  • Add an UI Image for Timer
    • Add Timer UI to “Source Image”
      • Press “Set Native Size”

 

4
Adding Timer Text
  • Add a UI Text for Timer Counter
    • Add Text for the default value
    • Change Font Style to “Bold”, Font Size to “30”
    • Add “Timer Controller” Script
    • Change Anchor to “Top-Right”

 

5
“TimerController” Script
  • Get the Reference for TimeUI
  • Claim “startTime”, “ellapsedTime” and “startCounter” as flag
  • Claim “minutes” and “seconds”
  • In “Start()” method
    • Set the flag to false
    • Get the Component of “Text”
  • Create a Function, “StartTimeCounter()” to start the Timer
    • Get the current time when the timer is started
    • Set the Flag to True
  • Create a Function, “StopTimeCounter()” to stop the Timer
    • Set the Flag to False
  • In “Update()” method,
    • if the flag is True (when it is on)
      • Get the Elapsed Time by subtracting “startTime” from the Current Time in this very frame
      • Get the minute and second based on the Elapsed Time
      • Update the Timer UI by accessing the Text Property with String.Format
6.1
“GameManager” Script
  • Add codes to Start and Stop the Timer
    • Get the Component of “TimerCounter” Class to use “StartTimeCounter()” and “StopTimeCounter()”
6.2
“Timer” Working

 

7.1
Importing “GameTitle”
  • Add “Game Title” image on the Hierarchy
    • Set the sorting layer to “Effect”

 

8
“GameManager” Script
  • Add Codes to Turn on & off Game Title by using “GameObject.SetActive()”
    • When Case “Opening”,
      • Set Game Title On
    • When Case “GamePlay”
      • Set Game Title Off
7.2
“GameTitle” Working

UNITY | PROJECT | SPACE SHOOTER 2D | 10. Game Scrolling BackGround

1
“EnemySpawner” Script
  • Add a code to reset “maxSpawnRateInSeconds” to 5
  • Refer to “GameManager” Script

 

2
Star Sprite & Script
3
Creating “Star” Prefab
4
“Star” Script
  • Claim “Speed” variable for the speed of “Star”
  • In “Update()” method
    • Get, Change and Set “transform.position” of “Star”, this script is attached to
    • in “if” statement,
      • if y position of “Star” goes down below the screen, change the position of “Star” on the top with x position random

 

5
Creating “StarGenerator” from EmptyGameObject | Adding “StarGenerator” Script

 

6.1
“StarGenerator” Scrip
  • Claim “MaxStars” variable for the number of Stars
  • Create “Color” Array for “Star”
  • In “Start()” method
    • Get the Scree
    • In “for” Loop,’
      • Instantiate “star”
      • Get Component “SpriteRenderer” to Star, instantiated from the prefab
      • Change “Color ” property “”of “SpriteRenderer”
        • color is changed with the calculation “[i % starColor.Length]”
      • Set the position of “Star” randomly
      • Set the speed with Random.value (generating a value between 0.0 and 1.0)
      • Set the position of Star in the position of “StarGenerator”

 

6.3
Prefab “Star”

 

 

20
“Star”
7
Adding “Sorting Layer”
8
Adding “Sorting Layer”
9
Setting “GamePlay” Layer to the GameObjects
10
Importing “Planet”

 

6.2
Creating “Planet” Prefab | Adding “Planet” Script
14
Creating “PlanetGenerator” from EmptyGameObject | Adding “PlanetController”
15
Setting Planet Prefab for each sprite
16
Setting Planet Prefab for each sprite
17
Setting Planet Prefab for each sprite
18
“Planet” Script
  • Claim “speed” variable for “Planet” speed
  • Claim “moving” as “Flag”
  • Claim the Screen as local variable
  • In “Awake()”
    • Set the Flag to false
    • Set the screen
  • In “Update()”
    • if “Planet” is not moving,
    • Change the position of Planet until the bottom of the screen
      • change the flag to false
  • In “ResetPostion()” method
    • Set the position of “Planet”  in the top of the screen with the x position random
19
“PlanetController” Script
  • Claim an Array of Planets as public
  • Create a Queue to hold “Planet”
  • In “Start()” method
    • Enqueue the planets in the queue
    • Invoke “MovingPlanetDown” every 20seconds
  • Create a Function, “EnqueuePlanets()”,
    • Create a “foreach” loop
      • if a planet’s position is below the screen and planet is not moving
        • Reset the Planet’s position by using “ResetPosition()” method of “Planet” class
        • Enqueue the disappearing  planet in the Queue
  • Create a Function, “MovePlanetDown”
    • Put “EnqueuePlanet()” method to collect Planets
    • Get a Planet from the queue by using “Dequeue()”
      • Get Component of “Planet” to set the flag to true
21
“Planet” Scrolling