๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90

 

 

 

Walk ์• ๋‹ˆ๋ฉ”์ด์…˜๋„ ์ƒ๊ฒผ๊ฒ ๋‹ค. ์ด์ œ ์บ๋ฆญํ„ฐ ์ขŒ์šฐ ์ด๋™์„ ๊ตฌํ˜„ํ•  ์ฐจ๋ก€๋‹ค. 2D ๊ฒŒ์ž„์ด๊ธด ํ•˜์ง€๋งŒ, ์ง€๊ธˆ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒŒ์ž„์€ ์ƒํ•˜ ์ด๋™์€ ์š”๊ตฌ๋˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ž˜์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ขŒ์šฐ ์ด๋™๋งŒ ๋จผ์ € ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

  • Rigidbody 2D์˜ Velocity(์†๋„)๋ฅผ ํ†ตํ•ด ์ขŒ์šฐ ์ด๋™ ๊ตฌํ˜„
  • Walk, Idle ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ ์ ˆํ•œ ํƒ€์ด๋ฐ์— ์‚ฌ์šฉํ•ด์ฃผ๊ธฐ
  • Transform์˜ Scale์˜ X ๊ฐ’์˜ ๋ถ€ํ˜ธ๋ฅผ ๋ฐ”๊ฟ”์„œ ์บ๋ฆญํ„ฐ๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉํ–ฅ ์ „ํ™˜ ๊ตฌํ˜„
 
 

1. ํ‚ค ์ž…๋ ฅ ๋ฐ›๊ธฐ

 

Girl Controller๋ผ๋Š” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์„œ Character ์˜ค๋ธŒ์ ํŠธ์— ๋ถ€์ฐฉํ•ด์คฌ๋‹ค.

 

์—ญ์‹œ, ๋‹ค ๋งŒ๋“ค๊ณ  ๊ธ€์„ ์“ฐ๋‹ˆ ์ˆœ์„œ๊ฐ€ ์กฐ๊ธˆ ์—‰๋ง์ด๋‹ค.

 

ํ‚ค ์ž…๋ ฅ์€ ์œ ๋‹ˆํ‹ฐ์—์„œ ์ œ๊ณตํ•˜๋Š” Input.GetAxisRaw() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ›์„ ๊ณ„ํš์ด๋‹ค.

Input.GetAxisRaw("Horizontal")๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ’์„ ๋ฆฌํ„ด ํ•ด์ค€๋‹ค.

  • Aํ‚ค ๋˜๋Š” ์™ผ์ชฝ ํ™”์‚ดํ‘œ ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด -1 ๋ฆฌํ„ด
  • Dํ‚ค ๋˜๋Š” ์˜ค๋ฅธ์ชฝ ํ™”์‚ดํ‘œ ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด 1 ๋ฆฌํ„ด
  • ํ‚ค ์ž…๋ ฅ์ด ์—†์œผ๋ฉด 0 ๋ฆฌํ„ด

 

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•œ ๋ณ€์ˆ˜์— ํ‚ค ์ž…๋ ฅ ๋ฆฌํ„ด ๊ฐ’์„ ์ €์žฅํ•ด๋‘˜ ๊ฒƒ์ด๋‹ค.

private float walkDirection;


void Update()
{
   walkDirection = Input.GetAxisRaw("Horizontal");
}

 

Rigidbody ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ†ตํ•œ ๋ฌผ๋ฆฌ ๊ธฐ๋ฐ˜์œผ๋กœ ์›€์ง์ž„์„ ๊ตฌํ˜„ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ‚ค ์ž…๋ ฅ์„ Update() ํ•จ์ˆ˜์—์„œ ๋ฐ›์•˜๋‹ค.

 

 

 

2. ์ขŒ์šฐ ์ด๋™ ๊ตฌํ˜„ํ•˜๊ธฐ

 

์‹ค์ œ๋กœ ๋ฌผ๋ฆฌ ๊ธฐ๋ฐ˜ ๋กœ์ง์ด ์ด๋ฃจ์–ด์ง€๋Š” ๋ถ€๋ถ„, ์ฆ‰ ์ด๋™์ด ์ง์ ‘์ ์œผ๋กœ ๊ตฌํ˜„๋˜์–ด์ง€๋Š” ๋ถ€๋ถ„์€ FixedUpdate() ํ•จ์ˆ˜์—์„œ ์ฒ˜๋ฆฌํ–ˆ๋‹ค. ํ‚ค ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•˜๋˜ "walkDirection" ๊ฐ’์€ -1, 0, 1 ๊ฐ’ ์ค‘ ํ•˜๋‚˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์œ„ ๋ฒกํ„ฐ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.

์—ฌ๊ธฐ์— ์†๋„ ์ œ์–ด๋ฅผ ์ฒ˜๋ฆฌํ•  ๋ณ€์ˆ˜ "walkSpeed"๋ฅผ ๊ณฑํ•ด์„œ ์บ๋ฆญํ„ฐ์˜ ์ด๋™์†๋„๋ฅผ ์กฐ์ ˆํ–ˆ๋‹ค.

public float walkSpeed;
private float walkDirection;
private Rigidbody2D _rigidBody;
...


void Start()
{
   _rigidBody = GetComponent<Rigidbody2D>();
   ...
}

void FixedUpdate()
{
   Walk();
}

private void Walk()
{
   bool hasControl = !Mathf.Approximately(walkDirection, 0f); // ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ฒ˜๋ฆฌ์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •
   _rigidBody.velocity = new Vector2(walkDirection * walkSpeed, _rigidBody.velocity.y);
}

 

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ–ˆ์œผ๋‹ˆ, ์ขŒ์šฐ ์ด๋™์€ ์šฐ์„  ๊ตฌํ˜„์ด ๋๋‹ค.

 

์บ๋ฆญํ„ฐ ์ขŒ์šฐ ์ด๋™(๋ฐฉํ–ฅ์ „ํ™˜, ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ ์šฉ ์ „)

 

ํ•˜์ง€๋งŒ, ์˜์ƒ์„ ๋ณด๋ฉด ์•Œ๊ฒ ์ง€๋งŒ ์•„์ง "Walk" ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ ์šฉํ•ด์ฃผ์ง€ ์•Š์•„, ๊ฐ€๋งŒํžˆ ์„œ์„œ ๊ฑท๋Š” ๋ชจ์Šต์„ ์ทจํ•˜๊ณ  ์žˆ๊ณ 

์ด๋™ ๋ฐฉํ–ฅ์„ ์ „ํ™˜ํ–ˆ์„ ๋•Œ, ์บ๋ฆญํ„ฐ๋„ ๋ฐ˜๋Œ€๋กœ ๋Œ์•„๋ณด์ง€ ์•Š๋Š” ์ ์ด ์•„์‰ฝ๋‹ค.

ํ•˜๋‚˜์”ฉ ์ฐจ๊ทผ์ฐจ๊ทผ ํ•ด๊ฒฐํ–ˆ๋‹ค. ์šฐ์„  ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ฒ˜๋ฆฌํ–ˆ๋‹ค.

 

 

 

3. Walk ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ ์šฉํ•ด์ฃผ๊ธฐ

 

์ €๋ฒˆ ๊ธ€์—์„œ ๋งํ–ˆ์ง€๋งŒ, ํ˜„์žฌ ์ฃผ์ธ๊ณต ์บ๋ฆญํ„ฐ๋Š” "Character"๋ผ๋Š” ๋นˆ ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์ตœ์ƒ๋‹จ ๋ถ€๋ชจ ์˜ค๋ธŒ์ ํŠธ์ด๊ณ ,

๊ทธ ๋ฐ‘์— "Girl"์ด๋ผ๋Š” ์‹ค์ œ ์บ๋ฆญํ„ฐ ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์žˆ๋‹ค.

 

๊ณ„์ธต ๊ตฌ์กฐ๊ฐ€ ํ˜„์žฌ ์ด๋Ÿฐ ์‹์œผ๋กœ ๋˜์–ด ์žˆ๋‹ค.

 

ํ˜„์žฌ Girl Controller ์Šคํฌ๋ฆฝํŠธ๋Š” "Character" ์˜ค๋ธŒ์ ํŠธ์—, ์• ๋‹ˆ๋ฉ”์ดํ„ฐ๋Š” "Girl" ์˜ค๋ธŒ์ ํŠธ์— ๋ถ€์ฐฉ๋˜์–ด ์žˆ๋Š” ์ƒํ™ฉ์ด๋‹ค.

๊ทธ๋ž˜์„œ GetComponentInChildren() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ž์‹์— ๋ถ™์–ด์žˆ๋Š” ์• ๋‹ˆ๋ฉ”์ดํ„ฐ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‹ด์•„์˜ฌ ๊ณ„ํš์ด๋‹ค.

public float walkSpeed;
private float walkDirection;
private Rigidbody2D _rigidBody;
private Animator animator;

void Start()
{
    _rigidBody = GetComponent<Rigidbody2D>();
    animator = GetComponentInChildren<Animator>();  // ์ถ”๊ฐ€
}

void FixedUpdate()
{
    Walk();
}

private void Walk()
{
    bool hasControl = !Mathf.Approximately(walkDirection, 0f);
    animator.SetBool("Walk", hasControl);           // ์ถ”๊ฐ€
    _rigidBody.velocity = new Vector2(walkDirection * walkSpeed, _rigidBody.velocity.y);
}

 

ํ‚ค ์ž…๋ ฅ์ด ์žˆ๋‹ค๋ฉด "Idle" → "Walk"๋กœ ์ƒํƒœ ์ „์ด๊ฐ€ ์ผ์–ด๋‚˜๊ณ , ํ‚ค ์ž…๋ ฅ์ด ์—†๋‹ค๋ฉด ๋‹ค์‹œ "Walk" → "Idle"์ด ๋  ๊ฒƒ์ด๋‹ค.

 

์บ๋ฆญํ„ฐ ์ขŒ์šฐ ์ด๋™(๋ฐฉํ–ฅ์ „ํ™˜ ๊ตฌํ˜„ ์ „)

 

์บ๋ฆญํ„ฐ๊ฐ€ ๋ฐ”๋‹ฅ์—์„œ ์กฐ๊ธˆ ๋ฏธ๋„๋Ÿฌ์ง€๋“ฏ ๊ฑธ์–ด์„œ, ์• ๋‹ˆ๋ฉ”์ด์…˜ ์žฌ์ƒ ์†๋„๋‚˜ ์บ๋ฆญํ„ฐ์˜ ์ด๋™ ์†๋„๋ฅผ ์กฐ์ ˆํ•ด์คฌ๋‹ค.

์ด์ œ ๋งˆ์ง€๋ง‰์œผ๋กœ ์บ๋ฆญํ„ฐ๊ฐ€ ๋ณด๋Š” ๋ฐฉํ–ฅ ์ „ํ™˜ ๊ตฌํ˜„๋งŒ ๋‚จ์•˜๋‹ค.

 

 

 

4. ์บ๋ฆญํ„ฐ๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉํ–ฅ ์ „ํ™˜ ๊ตฌํ˜„ํ•˜๊ธฐ

 

๋ณธ ๋ธ”๋กœ๊ทธ์˜ Unity ์นดํ…Œ๊ณ ๋ฆฌ์—์„œ 2D ์บ๋ฆญํ„ฐ๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉํ–ฅ ์ „ํ™˜์— ๋Œ€ํ•œ ๊ธ€์„ ๋‹ค๋ฃฌ ์ ์ด ์žˆ์—ˆ๋‹ค. ๋ณธ์ธ์€ Transform์˜ Scale์˜ X ๊ฐ’์˜ ๋ถ€ํ˜ธ๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋Š” ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ๊ตฌํ˜„ํ•ด์คฌ๋‹ค.

 

์ดˆ๊ธฐ Transform.Scale.x์˜ ๊ฐ’์„ ์ €์žฅํ•ด๋‘๊ณ , ์ด ๊ฐ’๊ณผ ๋‹จ์œ„ ๋ฒกํ„ฐ๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” "walkDirection" ๊ฐ’์„ ๊ณฑํ•œ ๊ฐ’์ด ๋‹ค๋ฅด๋‹ค๋ฉด ๋ฐฉํ–ฅ ์ „ํ™˜์„ ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์„ ์ƒ๊ฐํ–ˆ๊ณ , ์ด๋Œ€๋กœ ์ ์šฉํ–ˆ๋‹ค.

public float walkSpeed;
public Transform girlTransform;  // Transform์€ ์™œ GetComponentInChildren์ด ์ ์šฉ๋˜์ง€ ์•Š์„๊นŒ?
private Rigidbody2D _rigidBody;
private Animator animator;
private float walkDirection;
private float initScaleX;        // ์ถ”๊ฐ€


void Start()
{
    _rigidBody = GetComponent<Rigidbody2D>();
    animator = GetComponentInChildren<Animator>();
    initScaleX = girlTransform.localScale.x;    // ์ถ”๊ฐ€
}

void FixedUpdate()
{
    Walk();
}

private void Walk()
{
    bool hasControl = !Mathf.Approximately(walkDirection, 0f);
    TurnOtherSide(hasControl);                // ์ถ”๊ฐ€
    
    animator.SetBool("Walk", hasControl);
    _rigidBody.velocity = new Vector2(walkDirection * walkSpeed, _rigidBody.velocity.y);
}


private void TurnOtherSide(bool hasControl)  // ์ถ”๊ฐ€
{
    if (!hasControl)
       return;

    var scaleX = girlTransform.localScale.x;
    if (Mathf.Approximately(walkDirection * scaleX, initScaleX))
       return;
       
    var scaleY = girlTransform.localScale.y;
    var scaleZ = girlTransform.localScale.z;
    
    // ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์–ด์ ธ์„œ, ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ๋ณ€์ˆ˜์— ๋‹ด์•˜๋‹ค.
    girlTransform.localScale = new Vector3(-scaleX, scaleY, scaleZ);
}

 

์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ, ์ตœ์ข…์ ์œผ๋กœ ์บ๋ฆญํ„ฐ ์ขŒ์šฐ ์ด๋™์ด ๊ตฌํ˜„ ์™„๋ฃŒ๋˜์—ˆ๋‹ค.

 

์บ๋ฆญํ„ฐ ์ขŒ์šฐ ์ด๋™ ๊ตฌํ˜„ ์™„๋ฃŒ

 

์ด ์™ธ์—๋„ ๋‹ฌ๋ฆฌ๊ธฐ, ์ƒํ˜ธ์ž‘์šฉ, ์ ํ”„, ์ˆจ๊ธฐ ์ •๋„์˜ ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ๋” ์žˆ๋Š”๋ฐ ์–ธ์ œ ๋˜ ๋งŒ๋“ค์ง€,,, ์ฒœ์ฒœํžˆ ๋น ๋ฅด๊ฒŒ ํ•ด๋ณด์ž,,,ใ…Žใ…Žใ…Ž

ํ˜น์‹œ ๋ชฐ๋ผ ๋„์›€์ด ํ•„์š”ํ•˜์‹ค ๋ถ„๋“ค์„ ์œ„ํ•ด ์ „์ฒด ์†Œ์Šค์ฝ”๋“œ ์•„๋ž˜์— ์ฒจ๋ถ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋”๋ณด๊ธฐ

"Girl Controller" ์ „์ฒด ์†Œ์Šค์ฝ”๋“œ

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GirlController : MonoBehaviour
{

    public float walkSpeed;

    private Animator animator;
    private SpriteRenderer[] _sprite;
    public Transform girlTransform;
    private Rigidbody2D _rigidBody;
    private float walkDirection;
    private float initScaleX;

    void Start()
    {
        _rigidBody = GetComponent<Rigidbody2D>();
        //girlTransform = GetComponentInChildren<Transform>();  // Transform์€ ์ ์šฉ์ด ์•ˆ ๋˜๋„ค?
        _sprite = GetComponentsInChildren<SpriteRenderer>();
        animator = GetComponentInChildren<Animator>();          
        initScaleX = girlTransform.localScale.x;
    }

    void Update()
    {
        walkDirection = Input.GetAxisRaw("Horizontal");
    }


    void FixedUpdate()
    {
        Walk();
    }

    private void Walk()
    {
        bool hasControl = !Mathf.Approximately(walkDirection, 0f);
        TurnOtherSide(hasControl);
        animator.SetBool("Walk", hasControl);
        _rigidBody.velocity = new Vector2(walkDirection * walkSpeed, _rigidBody.velocity.y);
    }


    private void TurnOtherSide(bool hasControl)
    {
        if (!hasControl)
            return;

        var scaleX = girlTransform.localScale.x;
        if (Mathf.Approximately(walkDirection * scaleX, initScaleX))
            return;

        var scaleY = girlTransform.localScale.y;
        var scaleZ = girlTransform.localScale.z;

        girlTransform.localScale = new Vector3(-scaleX, scaleY, scaleZ);
    }
}

 

 

728x90
๋ฐ˜์‘ํ˜•