๐ŸŽฎ Unity Study/Unity

[Unity Associate Programmer ์ž๊ฒฉ์ฆ ์ค€๋น„] 1๊ฐ•. 3D ์ž๋™์ฐจ ์žฅ์• ๋ฌผ ํ”ผํ•˜๊ธฐ ๊ฒŒ์ž„ ๋งŒ๋“ค๊ธฐ

ibelieveinme 2023. 5. 9. 05:08
728x90

*Player ์‹œ์  ์ด๋™/ํšŒ์ „ ๋ฐฉ๋ฒ•

 

1) custom package ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

Asset > Import Package > Custom Package ์—์„œ package ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

2) Sceneํ™”๋ฉด์˜ Player ์‹œ์  ์ด๋™/ํšŒ์ „

์˜ค๋ฅธ์ชฝ ํด๋ฆญํ•œ ์ƒํƒœ์—์„œ WASD ํ‚ค๋ฅผ ์‚ฌ์šฉํ•ด ํ™”๋ฉด ํฌ์ปค์Šค ์ด๋™.

Alt ํ‚ค + ์˜ค๋ฅธ์ชฝ ๋“œ๋ž˜๊ทธ๋กœ ๋ณด์ด๋Š” ํ™”๋ฉด xyz ํšŒ์ „

Scene Tool์˜ View Tool ๊ณผ ๋™์ผํ•œ ๊ธฐ๋Šฅ

 

3) ๊ฐ์ฒด ๋‹น๊ฒจ์„œ ์ดˆ์  ๋งž์ถ”๊ธฐ

๊ฐ์ฒด๋ฅผ ์„ ํƒํ•œ ์ƒํƒœ์—์„œ ์”ฌ(Scene) ๋ทฐ์— ๋งˆ์šฐ์Šค ์ปค์„œ๋ฅผ ๋‘๊ณ  Fํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ ์ดˆ์ ์„ ๋งž์ถœ ์ˆ˜ ์žˆ๋‹ค.

๊ฐ์ฒด ๋”๋ธ” ํด๋ฆญํ•œ ๊ฒƒ๊ณผ ๊ฐ™์€ ํšจ๊ณผ

 

4) Trasform Position reset

Trasnform ์  3๊ฐœ ํด๋ฆญ > Reset Property > Position ํด๋ฆญ

 

5) Tools

View Tool: ๋“œ๋ž˜๊ทธํ•˜์—ฌ Player ์‹œ์  ์ด๋™/ํšŒ์ „

Move Tool: xyz ํ™”์‚ดํ‘œ๋กœ ๊ฐ์ฒด ์ด๋™

Rotate Tool: ๊ฐ์ฒด xyz ํšŒ์ „

Scale Tool: ํฌ๊ธฐ์กฐ์ ˆ

Rect Tool: ํฌ๊ธฐ, pivot ์กฐ์ ˆ

Transform Tool: move, rotate, rect ํˆด ๋ชจ๋‘๋ฅผ ํ•ฉํ•œ ๊ฒƒ

 

ํด๋ฆญ๋„ ๋˜์ง€๋งŒ QWERTY ํ‚ค๋กœ ์กฐ์ž‘ ๊ฐ€๋Šฅํ•˜๋‹ค.


*Player ์กฐ์ž‘ํ•˜๊ธฐ

1) ๋ฌผ๋ฆฌ ์ ์šฉ

๋ฌผ์ฒด์— ๋ฌผ๋ฆฌ๋ฅผ ์ ์šฉํ•˜๋ ค๋ฉด Rigidbody ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.

๋ฌผ์ฒด์— ์ถฉ๋Œ์„ ์ ์šฉํ•˜๋ ค๋ฉด Collider ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.

 

- Rigidbody: ๋ฌผ๋ฆฌ๋ ฅ์˜ ์ œ์–ด ํ•˜์— ๋™์ž‘ํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ.

Mass: Object์˜ ๋ฌด๊ฒŒ. kg๋‹จ์œ„.

Drag: ํž˜์„ ๋ฐ›์•„ ๋™์ž‘ํ•  ๋•Œ Object์— ๊ฐ€ํ•ด์ง€๋Š” ๊ณต๊ธฐ ์ €ํ•ญ๋ ฅ ์ •๋„. 0์€ ์ €ํ•ญ๋ ฅ์ด ์—†๋Š” ์ƒํƒœ.

Argular Drag: ํšŒ์ „๋ ฅ์— ์˜ํ•ด ํšŒ์ „์‹œ ์˜ค๋ธŒ์ ํŠธ์— ๊ฐ€ํ•ด์ง€๋Š” ๊ณต๊ธฐ ์ €ํ•ญ๋ ฅ์˜ ์ •๋„. 0์€ ์ €ํ•ญ๋ ฅ์ด ์—†๋Š” ์ƒํƒœ.

Use Gravity: ์ค‘๋ ฅ์˜ ์˜ํ–ฅ์„ ๋ฐ›์„ ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ์œ ๋ฌด

is Kinematic: ์™ธ๋ถ€์—์„œ ๊ฐ€ํ•ด์ง€๋Š” ๋ฌผ๋ฆฌ์  ํž˜์— ๋ฐ˜์‘ํ•˜์ง€ ์•Š๋Š” ์˜ค๋ธŒ์ ํŠธ๋ผ๋Š” ์˜๋ฏธ.

...

 

- Collider: ๋ฌผ๋ฆฌ ์ถฉ๋Œ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์˜ค๋ธŒ์ ํŠธ์˜ ํ˜•ํƒœ

is Trigger๋ฅผ ์ฒดํฌํ•˜์ง€ ์•Š์œผ๋ฉด ๋‹ค๋ฅธ collider๊ฐ€ ๋ฌผ์ฒด๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋Š” ์žฅ์• ๋ฌผ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ.

is Trigger๋ฅผ ์ฒดํฌํ•˜๋ฉด ๋‹ค๋ฅธ collider๊ฐ€ ํ†ต๊ณผ์‹œํ‚ค๋˜ ์ง€๋‚˜๊ฐ€๋Š” ๋ฌผ์ฒด๋ฅผ ๊ฐ์ง€ํ•จ.

 

2) ๋™๋ ฅ ๋งŒ๋“ค๊ธฐ

- transform ๊ฐ’ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜(๊ฐ์ฒด ์ด๋™)

//์•ž์œผ๋กœ ์ด๋™
transform.Translate(0, 0, 1);
transform.Translate(Vector3.forward);

์œ„ ์ฝ”๋“œ๋กœ ์‹คํ–‰ํ•˜๋ฉด Update๋ฌธ์—์„œ ์ปดํ“จํ„ฐ ์‚ฌ์–‘์— ๋”ฐ๋ผ ์ผ๊ด€๋˜์ง€ ์•Š๋Š” ์†๋„๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

 

-> ์ฐจ๋Ÿ‰์†๋„ ์กฐ์ ˆํ•˜๊ธฐ. ์ดˆ๋‹น ์›€์ง์ด๋„๋ก ํ•˜๊ธฐ.

Update() ๋ฌธ์€ ์ปดํ“จํ„ฐ ์‚ฌ์–‘์— ๋”ฐ๋ผ 1์ดˆ๋‹น 20-60 ํ”„๋ ˆ์ž„์„ ํ˜ธ์ถœํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ปดํ“จํ„ฐ ์‚ฌ์–‘์— ์ผ๊ด€๋œ ๊ฒฐ๊ณผ๋ฌผ์„ ๋ณผ ์ˆ˜ ์—†๋‹ค.

Time.deltaTime์€ ํ”„๋ ˆ์ž„ ๊ฐ„ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„์„ ์˜๋ฏธํ•œ๋‹ค. ์ด๋™๊ฐ’ * Time.deltaTime์„  update() ๋ฌธ์— ์‚ฌ์šฉํ•˜๋ฉด ํ”„๋ ˆ์ž„ ๊ฐ„ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„๋งŒ ์†๋„์— ์˜ํ–ฅ์„ ์ฃผ๋ฏ€๋กœ ํ”„๋ ˆ์ž„๊ณผ ๊ด€๊ฒŒ์—†์ด ์ผ๊ด€๋œ ์„ฑ๋Šฅ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

* Time.deltaTime: ๋งˆ์ง€๋ง‰ ํ”„๋ ˆ์ž„์—์„œ ํ˜„์žฌ ํ”„๋ ˆ์ž„๊นŒ์ง€์˜ ๊ฐ„๊ฒฉ(์ดˆ)

private void Update() {
    transform.Translate(Vector3.forward * Time.deltaTime * 20);
}

 

3) ์นด๋ฉ”๋ผ์˜ ์‹œ์„ ์„ ์ž๋™์ฐจ ์œ„๋กœ ๋”ฐ๋ผ๋‹ค๋‹ˆ๋„๋ก ํ•˜๋Š” ๋ฒ•

์นด๋ฉ”๋ผ์˜ ์œ„์น˜๋ฅผ player(์ž๋™์ฐจ) ์œ„์น˜๋กœ ๋งž์ถ”๋ฉด ๋˜๋Š”๋ฐ, ํ˜„์žฌ ์นด๋ฉ”๋ผ ์œ„์น˜๋งŒํผ ๋”ํ•œ ๊ฐ’์— ์œ„์น˜์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค.

Vehicle(์ž๋™์ฐจ)์˜ ์œ„์น˜๋Š” Vector3(0,0,0)์ธ๋ฐ Main Camera์˜ ์œ„์น˜๊ฐ€ Vector3(0,6,-10) ์ด๋ฏ€๋กœ ์นด๋ฉ”๋ผ์˜ ์œ„์น˜๋ฅผ ์ž๋™์ฐจ ์œ„์น˜์—์„œ (0,6,-10) ๋งŒํผ ๋”ํ•œ ์œ„์น˜๋กœ ์œ ์ง€์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค.

using UnityEngine;

public class FollowPlayer : MonoBehaviour
{
    [SerializeField] private GameObject player;
    private Vector3 offset = new Vector3(0, 6, -10);

    void Update(){
        transform.position = player.transform.position + offset;
    }
}

 

4) ๋ฒ„๋ฒ…์ด๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ

using UnityEngine;

public class FollowPlayer : MonoBehaviour
{
    [SerializeField] private GameObject player;
    private Vector3 offset = new Vector3(0, 6, -10);

    void LateUpdate(){
        transform.position = player.transform.position + offset;
    }
}

Update() vs LateUpdate()

Update(): ์ฐจ๋Ÿ‰๊ณผ ์นด๋ฉ”๋ผ๊ฐ€ ๋™์‹œ์— ์›€์ง์ž„

LateUpdate(): Update()๋ฌธ ์ดํ›„์— ํ˜ธ์ถœ๋จ. ์ฆ‰, Update()๋ฌธ์œผ๋กœ ์ฐจ๋Ÿ‰์ด ๋จผ์ € ์ด๋™ํ•œ ํ›„ LateUpdate()๋ฌธ์œผ๋กœ ์นด๋ฉ”๋ผ๊ฐ€ ์ฐจ๋Ÿ‰์„ ๋”ฐ๋ผ๊ฐ.

 

5) Play Mode ์ƒ‰๊น” ์ง€์ •ํ•˜๋Š” ๋ฒ•

mac ๊ธฐ์ค€. Unity > Preferences > Colors > Playmode tint

 

6) ํ‚ค๋ณด๋“œ ๋ฐฉํ–ฅํ‚ค๋กœ ์ขŒ์šฐ์ƒํ•˜ ์ด๋™ ์ ์šฉํ•˜๊ธฐ

mac ๊ธฐ์ค€ Edit > Project Setting > Input Manager > Axes > Horizontal

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private const float SPEED = 5.0f;
    private const float TURN_SPEED = 2f;
    private float horizontalInput;
    private float forwardInput;

    private void Update() {
        horizontalInput = Input.GetAxis("Horizontal");
        forwardInput = Input.GetAxis("Vertical");

        transform.Translate(Vector3.forward * Time.deltaTime * SPEED * forwardInput);
        transform.Translate(Vector3.right * Time.deltaTime * TURN_SPEED * horizontalInput);
    }
}

Vector3.forward = Vector3(0, 0, 1)

Vector3.right = Vector3(1, 0, 0)

 

Input.GetAxis() ํ•จ์ˆ˜ ์•ˆ์— Input ํ‚ค์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋˜๋Š”๋ฐ, Input Manager์— ์žˆ๋Š” Horizontal, Vertical์˜ Name๊ณผ ๋˜‘๊ฐ™์ด ์ž…๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

Update() ๋ฌธ์—์„œ horizontalInput, Vertical ๊ฐ’์„ ์ถ”๊ฐ€๋กœ ๊ณฑํ•ด์ค˜์„œ ๋ฐฉํ–ฅํ‚ค์™€ A,D,W,Sํ‚ค๋กœ ์ขŒ์šฐ์ƒํ•˜ ์ด๋™์ด ๊ฐ€๋Šฅํ•ด์กŒ๋‹ค.

์ฐธ๊ณ ) ์กฐ์ด์Šคํ‹ฑ ๊ฐ’์˜ ๋ฒ”์œ„๋Š” -1 ~ 1 ์‚ฌ์ด์˜ ๊ฐ’์ด ์ž…๋ ฅ๋œ๋‹ค.

 

์ฐจ๋Ÿ‰์„ ํšŒ์ „ํ•˜๋ฉฐ ์ขŒ์šฐ์ด๋™ํ•˜๊ณ  ์‹ถ์œผ๋ฉด Rotate() ํ•จ์ˆ˜๋„ ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ !

transform.Rotate(Vector3.up, Time.deltaTime * TURN_SPEED * horizontalInput);

 

728x90