๐ŸŽฎ Unity Study/Unity

[Unity Associate Programmer ์ž๊ฒฉ์ฆ ์ค€๋น„] 3๊ฐ•. ์žฅ์• ๋ฌผ ๋„˜๊ธฐ ๊ฒŒ์ž„(์• ๋‹ˆ๋ฉ”์ด์…˜, ํŒŒํ‹ฐํด, ์‚ฌ์šด๋“œ)

ibelieveinme 2023. 5. 21. 01:54
728x90
 

3๋‹จ์› - ์Œํ–ฅ ๋ฐ ํšจ๊ณผ - Unity Learn

์ด ๋‹จ์›์—์„œ๋Š” ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์ถฉ๋Œ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค๊ฐ€์˜ค๋Š” ์žฅ์• ๋ฌผ์„ ๋›ฐ์–ด๋„˜์œผ๋ฉฐ ๋น ๋ฅธ ์†๋„๋กœ ์ง„ํ–‰๋˜๋Š” ๋ฌดํ•œ ํšก์Šคํฌ๋กค ๋Ÿฌ๋‹ ๊ฒŒ์ž„์„ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•ฉ๋‹ˆ๋‹ค. ์ด ํ”„๋กœํ† ํƒ€์ž…์„ ์ œ์ž‘ํ•˜๋ฉด์„œ ์–ด๋–ป๊ฒŒ ์Œ์•…๊ณผ

learn.unity.com


1. Space๋ฐ” ๋ˆ„๋ฅผ ์‹œ, ์บ๋ฆญํ„ฐ๊ฐ€ ์ ํ”„ํ•˜๋Š” ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

์บ๋ฆญํ„ฐ์— ์ค‘๋ ฅ์ค˜์•ผ ํ•˜๋‹ˆ๊นŒ rigidbody ์ถ”๊ฐ€ํ•˜๊ณ  ~

using UnityEngine;

public class PlayerController : MonoBehaviour{

    const float jumpForce = 10f;
    const float gravityModifier = 3f;
    Rigidbody playerRb;

    void Start(){
        playerRb = transform.GetComponent<Rigidbody>();
        Physics.gravity *= gravityModifier;
    }

    void Update(){
        if (Input.GetKeyDown(KeyCode.Space)){
            playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
        }
    }
}

Space๋ฐ” ํด๋ฆญ ์‹œ, rigidbody ์— ํž˜์„ ์ถ”๊ฐ€ํ•ด์ฃผ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

public void AddForce(Vector3 force, ForceMode mode = ForceMode.Force);

//ํž˜์„ ์–ด๋””์— ์–ด๋–ป๊ฒŒ ์ž‘์šฉํ•˜๊ฒŒ ํ• ์ง€

 

* ForceMode(Force, Acceleration, Impulse, VelocityChange)

1) Force: ์—ฐ์†์ ์ธ ํž˜, ์งˆ๋Ÿ‰ ๋ฌด์‹œ ์•ˆํ•จ.

2) Acceleration: ์—ฐ์†์ ์ธ ํž˜, ์งˆ๋Ÿ‰ ๋ฌด์‹œํ•จ.

3) Impulse: ๋ถˆ์—ฐ์†์ ์ธ ํž˜, ์งˆ๋Ÿ‰๋ฌด์‹œ ์•ˆํ•จ. ์ฆ‰์‹œ์ ์šฉ์ด ๊ฐ€๋Šฅํ•จ.

4) VelocityChange:๋ถˆ์—ฐ์†์ ์ธ ํž˜, ์งˆ๋Ÿ‰ ๋ฌด์‹œํ•จ.

 

2. ์›€์ง์ด๋Š” ๋ฐฐ๊ฒฝ ๋งŒ๋“ค๊ธฐ

using UnityEngine;

public class MoveLeft : MonoBehaviour
{
    private const float SPEED = 15f;

    void Update() {
        transform.Translate(Vector3.left * Time.deltaTime * SPEED);
    }
}

MoveLeft ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฐฐ๊ฒฝ object ์— ์ถ”๊ฐ€ํ•œ๋‹ค.

Vector3.left = new Vector3(-1, 0, 0) ๊ฐ’์œผ๋กœ x๊ฐ’์„ -1์”ฉ ์ค„์—ฌ์„œ ์™ผ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๊ฒŒ ํ•˜๋Š” ๊ฐ’์ด๋‹ค.

 

3. ์žฅ์• ๋ฌผ ๋งŒ๋“ค๊ธฐ

์žฅ์• ๋ฌผ Prefab์„ Hierarchy์— ๋„์›Œ๋†“๊ณ  Collider์„ ์ฃผ๊ณ  Collider ๋ฒ”์œ„๋ฅผ ์ˆ˜์ •ํ•ด์„œ Prefabs ํด๋”์— ๋„ฃ๊ณ  Hierarchy ์—์„  ์‹น ์ง€์šด๋‹ค.

Obstacle ๋„ ํ”Œ๋ ˆ์ด์–ด๋ฅผ ํ–ฅํ•ด ์™€์•ผํ•˜๋ฏ€๋กœ MoveLeft ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋‹ฌ์•„์ค€๋‹ค.

using UnityEngine;

public class SpawnManager : MonoBehaviour
{
    [SerializeField] private GameObject[] obstaclePrefabs;
    private Vector3 spawnPos = new Vector3(25, 0, 0);

    private const float startDelay = 2f;
    private const float repeatRate = 2f;

    void Start() {
        InvokeRepeating("SpawnObstacle", startDelay, repeatRate);
    }

    private void SpawnObstacle() {
        int obstacleIndex = Random.Range(0, obstaclePrefabs.Length);
        Instantiate(obstaclePrefabs[obstacleIndex],
            spawnPos,
            obstaclePrefabs[obstacleIndex].transform.rotation,
            transform
            );
    }
}

SpawnManager ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  obstacle ๊ฐ์ฒด๋“ค์„ ๊ฐ–๊ณ  ์žˆ๋Š”๋‹ค.

InvokeRepeating ํ•จ์ˆ˜๋กœ 2์ดˆ๋งˆ๋‹ค ์ƒ์„ฑ๋˜๋„๋ก ํ•ด์ค€๋‹ค.

Player๊ฐ€ ์žฅ์• ๋ฌผ์— ๋‹ฟ์•˜์„ ๋•Œ particle์„ ์ค„๊ฑฐ๊ธฐ ๋•Œ๋ฌธ์— Player๊ฐ€ ๋„˜์–ด์ง€๊ฑฐ๋‚˜ ํšŒ์ „ํ•˜๋Š” ์ผ์ด ์—†๋„๋ก Rigidbody์˜ Constraints ๊ฐ’์„ ์ค€๋‹ค.

 

//Instantiate ํ•จ์ˆ˜์˜ ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜ ์ธ์ž๋ชจ์–‘๋“ค

 

4. ๋ฐฐ๊ฒฝ ๋ฐ˜๋ณต ์ƒ์„ฑํ•˜๊ธฐ

์ฃผ์–ด์ง„ ๋ฐฐ๊ฒฝ์ด๋ฏธ์ง€๋Š” ๋˜‘๊ฐ™์€ ์ด๋ฏธ์ง€๋ฅผ ์ด์–ด๋ถ™์ธ ๊ธด ์ด๋ฏธ์ง€์ด๋‹ค.

์ฆ‰ ์ค‘์‹ฌ์„ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„๋ฉด ๋˜‘๊ฐ™์€ 2๊ฐœ์˜ ์ด๋ฏธ์ง€๊ฐ€ ๋œ๋‹ค.

์ด๋ฅผ ์ด์šฉํ•ด์„œ ์ค‘์‹ฌ์ ์„ ์ง€๋‚  ๋•Œ, ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์‹œ ์ฒ˜์Œ ์œ„์น˜๋กœ ๋ณด๋‚ด๋ฉด ๋ฐ˜๋ณต๋˜๋Š” ๋ฐฐ๊ฒฝ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

์ด ๋•Œ, ํ•˜๋“œ์ฝ”๋”ฉ ๊ฐ’์œผ๋กœ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ์ผ์ • ์ง€์ (์ค‘๊ฐ„)์„ ์ง€๋‚  ๋•Œ, ๊ทธ ๋ถ€๋ถ„๋ถ€ํ„ฐ ๋‹ค์‹œ ์ด๋ฏธ์ง€ ์˜ฎ๊ฒจ์„œ ์‹œ์ž‘ํ•˜๋„๋ก ํ•  ์ˆ˜๋„ ์žˆ์œผ๋‚˜ ๋Š๊ฒจ๋ณด์ด๋Š” ํ˜„์ƒ์ด ์žˆ๋‹ค.

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ •ํ™•ํ•œ ๋ฐ˜ ๊ธธ์ด๋ฅผ ์•Œ์•„์•ผ ํ•œ๋‹ค.

์ด๋ฏธ์ง€์˜ ๋ฐ˜ ํฌ๊ธฐ๋Š” Box Collider์˜ x size ๊ฐ’์„ 2๋กœ ๋‚˜๋ˆ„๋ฉด ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

using UnityEngine;

public class RepeatBackground : MonoBehaviour{

    private Vector3 startPos;
    private float repeatWidth;

    void Start(){
        startPos = transform.position;
        repeatWidth = transform.GetComponent<BoxCollider>().size.x / 2;
    }

    void Update(){
        if(transform.position.x < startPos.x - repeatWidth) {
            transform.position = startPos;
        }
    }
}

RepeatBackground ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งŒ๋“ค๊ณ , ์‹œ์ž‘์ง€์ (startPos)๊ณผ ๋ฐ˜๋ณต ๊ฐ€๋กœ๊ฐ’(repeatWidth)์„ ๋ณ€์ˆ˜๋กœ ๋‘๊ณ  Start๋ฌธ์—์„œ ์„ค์ •ํ•ด์ค€๋‹ค.

Update() ๋ฌธ์—์„œ ๋ฐฐ๊ฒฝ์ด๋ฏธ์ง€์˜ ๊ธธ์ด๊ฐ€ ๋ฐ˜์„ ์ง€๋‚  ๋•Œ๋ฅผ ์ฒดํฌํ•ด์„œ ๋ฐฐ๊ฒฝ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์‹œ ์‹œ์ž‘์ ์œผ๋กœ ๋ณด๋‚ธ๋‹ค.

=> ์ด๋ฏธ์ง€๋ฅผ ๋ฐ˜ ์ž˜๋ผ์„œ ์ด๋ฏธ์ง€ ๋‘ ๊ฐœ๋ฅผ ์ด์šฉํ•ด์„œ ์œ„์น˜๋ฅผ ๊ณ„์† ๋ฐ”๊ฟˆ์œผ๋กœ์จ ๊ตฌํ˜„๋„ ๊ฐ€๋Šฅํ•˜๋‹ค!

 

5. ํƒœ๊ทธ๋กœ ์ถฉ๋Œ ๊ฐ์ฒด ์ฒดํฌํ•˜๊ณ  GameOver ์ฒดํฌํ•˜๊ธฐ

Tag > Add Tag์—์„œ Ground์™€ Obstacle ๋กœ ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค๊ณ  ๊ฐ ๊ฐ์ฒด์— ์ถ”๊ฐ€ํ•ด์ฃผ์ž.

 

//PlayerController.cs

    private void OnCollisionEnter(Collision collision){
        if(collision.gameObject.CompareTag("Ground")) {
            isOnGround = true;
        }
        else if (collision.gameObject.CompareTag("Obstacle")) {
            isGameOver = true;
        }
    }

playerController ํด๋ž˜์Šค์—์„œ OnCollisionEnter ํ•จ์ˆ˜๋กœ ์ถฉ๋Œ์ฒดํฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ ํ”„ ํ›„ Ground๋กœ ๋–จ์–ด์กŒ์„ ๋•Œ isOnGround = true๋กœ ์ฒดํฌํ•ด์ฃผ๊ณ 

Obstacle๊ณผ ์ถฉ๋Œํ–ˆ์„ ๋•Œ gameOver = true๋กœ ์ฒดํฌํ•ด์ฃผ์ž.

 

//PlayerController.cs

    public bool GetGameOver(){
        return isGameOver;
    }

 

//MoveLeft.cs

using UnityEngine;

public class MoveLeft : MonoBehaviour{
    private const float SPEED = 15f;

    void Update(){
        if (!PlayerController.Instance.GetGameOver()){
            transform.Translate(Vector3.left * Time.deltaTime * SPEED);
        }
    }
}

PlayerController.Instance.GetGameOver = true ์ด๋ฉด Game Over ์ƒํƒœ

PlayerController.Instance.GetGameOver = false ์ด๋ฉด Game Play ์ƒํƒœ

using UnityEngine;

public class SpawnManager : MonoBehaviour{
    [SerializeField] private GameObject[] obstaclePrefabs;
    private Vector3 spawnPos = new Vector3(25, 0, 0);

    private const float startDelay = 2f;
    private const float repeatRate = 2f;

    void Start(){
        InvokeRepeating("SpawnObstacle", startDelay, repeatRate);
    }

    private void SpawnObstacle(){
        if (PlayerController.Instance.GetGameOver()) return;

            int obstacleIndex = Random.Range(0, obstaclePrefabs.Length);
        Instantiate(obstaclePrefabs[obstacleIndex],
            spawnPos,
            obstaclePrefabs[obstacleIndex].transform.rotation,
            transform
            );
    }
}

 

GameOver ์ƒํƒœ์ด๋ฉด ๋ฐฐ๊ฒฝํ™”๋ฉด/์žฅ์• ๋ฌผ ์›€์ง์ž„, ์žฅ์• ๋ฌผ ์ƒ์„ฑ์„ ๋ชจ๋‘ ๋ฉˆ์ถ”์ž.

 

6. ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ ์šฉํ•˜๊ธฐ

1) ๋‹ฌ๋ฆฌ๊ธฐ ์• ๋‹ˆ๋ฉ”์ด์…˜

Player์— ๋‹ฌ๋ ค์žˆ๋Š” SimpleCharacter ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ํ™•์ธํ•ด๋ณด์ž.

P.S) ํ™”๋ฉด์ด ์งค๋ ค์„œ ์•ˆ๋ณด์ด๋ฉด Alt ํ‚ค ๋ˆ„๋ฅด๊ณ  ๋“œ๋ž˜๊ทธํ•˜๋ฉด ์ด๋™๋จ.

1)-1 ํ•œ ํ™”์‚ดํ‘œ๋“ค์„ ํด๋ฆญํ•˜๋ฉด ์ƒํƒœ๋ณ€ํ™”๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์œ„ ๊ทธ๋ฆผ์—์„  Idle -> Wark_Static ์ƒํƒœ๋กœ ๋ฐ”๋€Œ๋Š” ๋ชจ์Šต์ด ๋ณด์ธ๋‹ค.

๋ฐ”๋€Œ๋Š” ์กฐ๊ฑด์€ Static_b ๊ฐ’์ด true์ด๊ณ  Speed_f ๊ฐ’์ด 0.25 ์ด์ƒ์ผ ๋•Œ!

 

1)-2 ํ•œ ์ƒํƒœ๋ฅผ ๋”๋ธ”ํด๋ฆญํ•˜๋ฉด ํ•ด๋‹น ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ๋ณด์ธ๋‹ค.

์œ„ ์‚ฌ์ง„์€ Run ์ƒํƒœ๋ฅผ ๋”๋ธ”ํด๋ฆญํ•˜์—ฌ ๋‹ฌ๋ฆฌ๋Š” ๋ชจ์…˜์ด ํ™•์ธ๋œ๋‹ค.

 

1)-3 ์• ๋‹ˆ๋ฉ”์ด์…˜ ๊ธฐ๋ณธ์ƒํƒœ ๋ณ€๊ฒฝ

์บ๋ฆญํ„ฐ๊ฐ€ ๊ฒŒ์ž„ ์‹œ์ž‘๋•Œ ๋ฐ”๋กœ ๋‹ฌ๋ฆฌ๋Š” ๋ชจ์…˜์„ ์ทจํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ์ƒํƒœ๋ฅผ Idle -> Run_Static ์ƒํƒœ๋กœ ๋ฐ”๊พธ์ž.

๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ํ›„ Set as Layer Defualt Status ๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋œ๋‹ค.

๋˜ํ•œ ์™ผ์ชฝ์— Speed_f ์˜ ๊ฐ’์„ 1.0 ์ •๋„๋กœ ์˜ฌ๋ ค์ฃผ๊ณ  Insperctor ์ƒ์—์„œ Speed๋ฅผ ๋ฐฐ๊ฒฝ๊ฐ’๊ณผ ๋งž์ถฐ์ฃผ์ž.

 

๋‹ฌ๋ฃŒ๋‹ฌ๋ฃŒ

2) ์ ํ”„ ์• ๋‹ˆ๋ฉ”์ด์…˜

using UnityEngine;

public class PlayerController : MonoBehaviour{

    private Animator playerAnim;

    void Start(){
        playerAnim = transform.GetComponent<Animator>();
    }
    void Update(){
        if (Input.GetKeyDown(KeyCode.Space) && isOnGround) {
        	playerAnim.SetTrigger("Jump_Trig");
        }
    }
}

Run -> Running_Jump ์ƒํƒœ๋กœ ๋ณ€๊ฒฝํ•˜ํ•˜๊ธฐ ์œ„ํ•ด ํ™”์‚ดํ‘œ๋ฅผ ๋ˆŒ๋Ÿฌ๋ณด์ž.

Run -> Running_Jump ์ƒํƒœ๋กœ ๋ฐ”๋€Œ๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์€ Jump_trig์ด๋‹ค.

Trigger๋Š” animator.SetTrigger("ํŠธ๋ฆฌ๊ฑฐ๋ช…")์œผ๋กœ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰, ์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ Animator ๋ฅผ ์„ ์–ธํ•˜๊ณ  Space๋ฅผ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค Animator์—Run -> Runnig_Jump ์กฐ๊ฑด์ธ Jump_trig๋ฅผ Trigger๋กœ ์ฃผ๋ฉด ๋œ๋‹ค.

์ข€ ๋” ์ž์—ฐ์Šค๋Ÿฌ์šด ์ ํ”„๋ฅผ ํ•˜๋ ค๋ฉด ์ ํ”„ํ•  ๋•Œ๋Š” Jump ์• ๋‹ˆ๋ฉ”์ด์…˜ Speed๋ฅผ ์กฐ๊ธˆ ์ค„์—ฌ์ฃผ์ž. 1 -> 0.7๋กœ.

๊ทธ๋Ÿผ ์ ํ”„ ๋ชจ์…˜์„ ์ข€ ๋” ๊ธธ๊ฒŒ ํ•ด์„œ ์ž์—ฐ์Šค๋Ÿฝ๋‹ค.

๋‚ ๋ผ๋‚ ๋ผ

3) ์žฅ์• ๋ฌผ์— ๋ถ€๋”ชํ˜€์„œ ์“ฐ๋Ÿฌ์ง€๋Š” ๋ชจ์…˜

Depth ๋ ˆ์ด์–ด๋ฅผ ๋ณด๋ฉด, Alive -> Depth_01 ํ˜น์€ Alive -> Depth_02 ๋ชจ์…˜์ด ๋ณด์ด๋Š”๋ฐ

๋ณ€ํ™˜ ์กฐ๊ฑด์€ Depth_b ๊ฐ€ true์ผ ๋•Œ, ๋˜ํ•œ Type์€ id ๋กœ 1, 2 ์ค‘์— ๊ณจ๋ผ์•ผ ํ•œ๋‹ค.

1์€ ๋’ค๋กœ, 2๋Š” ์•ž์œผ๋กœ ์“ฐ๋Ÿฌ์ง€๋Š”๊ฑฐ๋ผ 1๋กœ ์ง€์ •ํ•ด์ฃผ์ž.

    private void OnCollisionEnter(Collision collision){
        if(collision.gameObject.CompareTag("Ground")) {
            isOnGround = true;
        }
        else if (collision.gameObject.CompareTag("Obstacle")) {
            isGameOver = true;
            playerAnim.SetBool("Death_b", true);
            playerAnim.SetInteger("DeathType_int", 1);
        }
    }

PlayerController ์Šคํฌ๋ฆฝํŠธ์—์„œ ์žฅ์• ๋ฌผ๊ณผ ์ถฉ๋Œํ–ˆ์„ ๋•Œ, SetBool, SetInteger๋กœ Depth_01 ๋ชจ์…˜์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค์ด๋‹ค์ด

* ์• ๋‹ˆ๋ฉ”์ด์…˜ ํŒŒ๋ผ๋ฏธํ„ฐ

https://docs.unity3d.com/kr/2021.3/Manual/AnimationParameters.html

 

์• ๋‹ˆ๋ฉ”์ด์…˜ ํŒŒ๋ผ๋ฏธํ„ฐ - Unity ๋งค๋‰ด์–ผ

์• ๋‹ˆ๋ฉ”์ด์…˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์• ๋‹ˆ๋ฉ”์ดํ„ฐ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์ •์˜๋œ ๋ณ€์ˆ˜๋กœ์„œ, ์Šคํฌ๋ฆฝํŠธ์—์„œ ์ ‘๊ทผํ•˜๊ณ  ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์Šคํฌ๋ฆฝํŠธ๋Š” ์ƒํƒœ ๋จธ์‹ ์„ ์ œ์–ดํ•˜๊ฑฐ๋‚˜ ํ๋ฆ„์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Šต

docs.unity3d.com

์• ๋‹ˆ๋ฉ”์ด์…˜ ์ „ํ™˜์กฐ๊ฑด์— ๋งž๊ฒŒ SetFloatSetIntegerSetBoolSetTrigger ๋ฐ ResetTrigger ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

7. Particle ์ ์šฉํ•˜๊ธฐ

Particle์„ Hierarchy ์ƒ์— ์˜ฌ๋ ค๋†“์œผ๋ฉด Particle์„ ์žฌ์ƒํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

Particle์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์†์„ฑ๊ฐ’. Speed, Size, Rotation, Color ๋“ฑ๋“ฑ...

public class PlayerController : MonoBehaviour{

    [SerializeField]
    private ParticleSystem explosionParticle;
    
    private void OnCollisionEnter(Collision collision){
        if (collision.gameObject.CompareTag("Obstacle")) {
            explosionParticle.Play();
        }
    }
}

์‹คํ–‰์‹œํ‚ค๋Š” ๋ฒ•์€ ParticleSystem ์„ ์–ธํ•˜๊ณ  ๊ฐ์ฒด ์—ฐ๊ฒฐ ํ›„ Play() ํ•จ์ˆ˜ ํ˜ธ์ถœํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

8. ์‚ฌ์šด๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ

1) ๋ฐฐ๊ฒฝ์Œ์•… ์ถ”๊ฐ€

sound ํŒŒ์ผ ํ•˜๋‚˜๋ฅผ ํด๋ฆญํ•˜๋ฉด ์‚ฌ์šด๋“œ๋ฅผ ๋“ค์–ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

Sound๋ฅผ ์žฌ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” Audio Listener, Audio Source ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•„์š”ํ•œ๋ฐ ๊ธฐ๋ณธ์ ์œผ๋กœ Audio Listener์€ Main Camera์— ๋‹ฌ๋ ค ์žˆ๋‹ค. ์ถ”๊ฐ€๋กœ Audio Source๋ฅผ ๋‹ฌ์•„์ฃผ์ž.

Audio Source์˜ AudioClip ์— ์‚ฌ์šด๋“œ ํŒŒ์ผ์„ ์—ฐ๊ฒฐํ•˜๊ณ  Volume์„ ์กฐ์ ˆํ•ด์ฃผ์ž.

 

*AudioListener: AudioSource์— ์žˆ๋Š” AudioClip์„ ์žฌ์ƒ์‹œํ‚ค๋Š” ์ปดํฌ๋„ŒํŠธ

*AudioSoucre: AudioํŒŒ์ผ์„ ์—ฐ๊ฒฐํ•˜๊ณ  Auio ํŒŒ์ผ์˜ ์†์„ฑ ๋ฐ ์„ค์ •์„ ์ ์šฉํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ

 

2) ๋ชจ์…˜ ํšจ๊ณผ์Œ ์ถ”๊ฐ€

using UnityEngine;

public class PlayerController : MonoBehaviour{

    [SerializeField] private AudioClip jumpSound;
    [SerializeField] private AudioClip crashSound;
    private AudioSource playerAudio;

    void Start(){
        playerAudio = transform.GetComponent<AudioSource>();
    }

    void Update(){
        if (Input.GetKeyDown(KeyCode.Space) && isOnGround && !isGameOver) {
            playerAudio.PlayOneShot(jumpSound, 1f);
        }
    }

    private void OnCollisionEnter(Collision collision){
        if (collision.gameObject.CompareTag("Obstacle")) {
 
            playerAudio.PlayOneShot(crashSound, 0.5f);
        }
    }
}

 

public void PlayOneShot (AudioClip clip, float volumeScale= 1.0F);

 

Player ๊ฐ์ฒด์— AudioSource ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‹ฌ์•„์ฃผ๊ณ , ์žฌ์ƒ์„ ์›ํ•˜๋Š” AudioClip๋ฅผ ์Šคํฌ๋ฆฝํŠธ์™€ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

AudioClip ์žฌ์ƒํ•จ์ˆ˜๋Š” PlayOneShot() !

728x90