0% found this document useful (0 votes)
14 views35 pages

겜프 5-1

Uploaded by

cracraty159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views35 pages

겜프 5-1

Uploaded by

cracraty159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Jonghyeok Park

Division of Computer Engineering,


Hankuk University of Foreign Studies (HUFS)

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Goal
• Rolling ball in Tilemap

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Preparation (1)
• Download the texture assests
– https://drive.google.com/file/d/1yeDXX0yhCZsZDuCMBNadhh2pDZ-Zi_Lz/view
– Also, download the resource.zip in E-class

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Preparation (2)
• Create Directories in Assets
– Crate > Folder
– “Textures”, “Materials”, “Scripts”, “Prefebs”

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Preparation (3)
• Copy the *.png file in resource
– Drag and Drop
– Right click on “Textures” > ”Show in explorer”
§ Copy the *.png files

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Preparation (4)
• Create Ground and Ground Material
1. Create > 3D Object > Cube
2. Name it as “Ground”
3. Create > Materials in Materials directory
4. Name it as “Ground”
5. Change the Albedo of Groudn Material to “Ground.png”

* Albedo: 물체가 받은 광원에 대한 반사율, 보통 Base color로 표현됨.

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Preparation (5)
• Set Ground Material
1. See materials in Mesh Render component
2. Set Ground material

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Preparation (6)
• Set Ground Material
1. See materials in Mesh Render component
2. Set Ground material
3. Set scale to (20 x 0.1 x 20)

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Preparation (7)
• Set Ground Material
1. See materials in Mesh Render component
2. Set Ground material
3. Set scale to (20 x 0.1 x 20)
4. Set tiling to (4x4) in Ground Material component

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Preparation (8)
• Create Player (ball)
1. Create > 3D Object > Sphere
2. Name it as “Player”
3. Set Transform.position as (0,0.55,0)

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park
User Input (1)
• Create “Ground.cs” script in Scripts directory
1. Create > C# Script
2. Name it as Ground.cs
3. Add Ground script to Ground object

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


User Input (2)
• Input.GetAxis(string axisName)
– Detect and collect user input
– Return the value of virtual axiss (i.e., between -1 and 1) identified by axisName
§ By defaults, “horizontal” and “vertical”
– By returning smooth valeus based on user input it is used to implement the
movement, rotation, and other actions of game object
– It is called every time the user changes the input value on an input device (e.g.,
keyboard, joystic, etc), and returns differences between the previous input value

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


User Input (3)
• Type arrow keys and check the console value
– What values are printed to the console?

- 1 ~ 1

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


User Input (4)
• Change the rotation by user input
– Use localEulerAngles()

“Ground moves in the opposite direction of the key input by user”

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


User Input (5)
• Change the rotation by user input
– Use localEulerAngles()

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


User Input (6)
• Input.GetKeyDown(string name)
– Detect user input and is called when the user presses a specific key
– It is called only once and is not called while the user holds down the key
§ Vs. Input.GetKeyUp(), Input.GetKey()
§ How can we implement character movement using WASD keys
– It allows for processing when user presses multiple keys simultaneously
§ E.g., Detect when the user presses the A key while holding down the Shift key

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


User Input (7)
• Jump the ball using Space key
1. Create “Player.cs” in Scripts directory
2. Add Rigidbody component
3. Add “Player.cs” script component in Player

”Deos it work”?

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


User Input (8)
• Jump the ball using Space key
1. Create “Player.cs” in Scripts directory
2. Add Rigidbody component
3. Add “Player.cs” script component in Player
4. Increase the Force value

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


User Input (9)
• Input.touchCount
– Detec and process touch input on mobile device
– Returns the number of fingers currently touching the screen in the current frame
– Returns integer value – 0 means fingers are touching the screen
– Called every frame to detect user’s toch input
§ GetTouchDown – Get more detailed information about individual touches
§ GetTouch – Return touch object that contains information about a specific touch

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


User Input (10)
• Input.GetMouseButton(int button)
– Detect when the mouse button is pressed or held down by the user.
– Takes a button identifier as an argument and returns true when the
corresponding mouse button is pressed or held down.
– Button parameter
§ 0 : left mouse button
§ 1 : right mouse button
§ 2 : middle mouse button
– GetMouseButtonDown(), GetMouseButtonUp()

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


User Input (11)
• Detect mouse button and position

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


User Input (12)
• Ground control using Mouse
– Change z rotaion using { Left | Right} mouse click

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Parent & Child Object
• Parent-Child hierarchical structure
• Creating a parent-child relationship where changes to the parent game
object's Transform component affect the child game object's
Transform component.
– Game objects with a parent-child relationship share the Transform component,
so changes to the parent game object's Transform component are propagated to
the child game object's Transform component.
§ This makes it easy to simplify the organization of game objects and implement
interactions between game objects.
– Parent-child objects simplify game object composition and make it easier to
implement interactions between game objects.
§ E.g., In character modeling, the body become parent game object and head, arms, legs,
become child object, and this makes it easy to implement motion

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Parent & Child Object
• Make a hierarchical relationship : Stage <-> {Ground, Obstacle}
1. Crate 3D object > Cyilinder , and name it as Obstacle
2. Change Obstacle’s position (2,0,4)
3. Create Game Object , and name it as Stage
4. Drage & Drop
§ Move Ground and Obstacle objects to Stage
5. Remove “Ground.cs” script in Ground
6. Add “Ground.cs” script in Stage

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Parent & Child Object
• Make a hierarchical relationship : Stage <-> {Ground, Obstacle}

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Local Position
• Rleative position of child game object to its parent game object
– It represents how far a game object is from its parent game object
– E.g., parent (0,0,0) child (1,0,0)
§ Child object is one uint away from the parent in the X-axis direction
• By adjusting the local position value, the position of a child game
object can be moved relative to its parent game object.
– if the parent game object is moved, the child game object will move with it.
– Local position can also be used to implement other actions, such as rotating or
scaling a child game object.

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Local Position
• Move the Obstacle object and check the position
– Using transform
1. Create “Obstacle.cs” script
2. Add “Obstacle.cs” script to Obstacle

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Local Position
• Move the Obstacle object and check the position
– Set “Local” and “Global”

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


WARNING: Unset script
• We do not need stage control and palyer control in this practice.
• Please unset the “Ground.cs” script of stage
• Please unset the “Player.cs” script of player

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Player Controller
• Move player using keyboard input
1. Create “Movement3D.cs” in the script directory
2. Create “PlayerController.cs” in the script directory
3. Add both scripts components to Player object

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Player Controller

Movement3D.cs PlayerController.cs

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Serialized Field
• Attribute that allows a private variable to be edited in the Inspector
window
– By default, Inspector window can only modify the public variables
– By adding the [SerializeField] attribute to a variable, even private variables can be
edited in the Inspector window.

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Awake() vs. Start()
• The Awake() function is called when a game object is created and
allocated to memory.
– It is called before the Start() function and is suitable for initializing game
objects.
– The Awake() function is called only once during the game object's
lifecycle, making it useful for initializing the game object.
– Suit for initializing object

• The Start() function is called after the Awake() function and is called
when the game object is enabled.
– The Awake() function and Start() function are used to perform
initialization work before the game object is enabled, and additional
initialization work immediately after the game object is enabled.
– Suit for additional initialization work after the game object is enabled

• The Awake() function is called only once during the game object's
lifecycle, while the Start() function is called every time the game
object is enabled.

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


References
• https://docs.unity3d.com/560/Documentation/Manual/
• 리소스
– https://www.youtube.com/watch?v=VFHAF_uKAAc

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park

You might also like