유니티

유니티 코인 먹기 게임 만들기 - 2 캐릭터 움직이기

당혜성 2025. 3. 27. 21:02

캐릭터 만들기

보통은 캡슐을 만든다. 사람하고 비슷해서 인거같다. 캡슐을 만들어주도록 하자.

캐릭터를 만들었다. 상당히 귀엽다.

 

물리를 추가해주자. 물리는 Rigidbody이다.

중력의 영향을 받지 않을거니까 중력은 끄고, Constraints는 자동으로 회전하는 것이다.

궁금하다면 하나 물체를 만들고 Rigidbody를 추가 후, 우리 귀여운 캐릭터위에 두고 실행해보자. 그럼 부딪히면서 둘 다 쓰러질 것이다. 이것을 방지하기 위해 Freeze Rotation 체크하도록 하자.

 

Player 스크립트

다시 폴더를 하나 만들고, C# 스크립트를 만든 후, 자기 캐릭터에게 c#스크립트를 끌어 넣자.

그럼 자기 캐릭터에 스크립트가 넣어졌을 것이다. 후에 스크립트를 켜주도록 하자.

 

다음 코드를 입력할건데 이렇게 만들어 주자.

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

public class palyer : MonoBehaviour
{
    public Rigidbody rbody;
    
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        print(h);
    }
}

 

Axis 가 키보드인데, Input을 해서 키보드를 입력받는다는 뜻이다. 이렇게 만들고 저장후, 유니티로 가서 한번 a d를 눌러보자. 그러면 왼쪽은 -1 오른쪽은 1이 나올것이다.

 

확인은 콘솔가서 확인하면 된다.

 

이번에는 위 아래를 만들어 보자.

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

public class palyer : MonoBehaviour
{
    public Rigidbody rbody;
    
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
        print(v);
    }
}

이렇게 만들고 다시 유니티에 가서 w를 누르면 1 s를 누르면 -1이 나온다.

 

다음 벡터를 만들어 보도록 하자.

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

public class palyer : MonoBehaviour
{
    public Rigidbody rbody;
    public Vector3 moveDir;
    
    void Update()
    {
        moveDir = new Vector3(); 
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
        print(v);
    }
}

 

어떻게 움직일지 정해주는 거라고 보면 된다. 전에 설명했듯이 백터는 x y z축을 움직인다. 그러면 Vector3안에 상 하 좌 우를 움직이는 코드를 넣도록 하자.

 

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

public class palyer : MonoBehaviour
{
    public Rigidbody rbody;
    public Vector3 moveDir;
    
    void Update()
    {
        moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); 
    }
}

이렇게 넣어주면 완성이다. x축은 Input.GetAxisRaw("Horizontal") 만큼 움직일 거니까 넣어주고, y축은 위로 가는건데 위로는 안가니까 0을 넣어주고, z축은 Input.GetAxisRaw("Vertical") 만큼 움직일 거니까 넣는다.

저장 후 유니티로 가자.

 

플레이어 스크립트에 리지드바디만 넣어주도록 하자. 그리고 실행 후 움직여 보도록 하자.

 

 

움직이면 x z축이 -1 0 1로 반응하게 된다. 이제 물리적으로 움직여 보도록 하자.

 

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

public class palyer : MonoBehaviour
{
    public Rigidbody rbody;
    public Vector3 moveDir;
    
    void Update()
    {
        moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); 
    }

    private void FixedUpdate()
    {
        rbody.MovePosition(rbody.position + moveDir);
    }
}

rbody 움직임(rbody의 포지션 + 벡터값의 움직임) 이다.

수학 수식으로 하면 rbody 움직임 = rbody의 포지션 + 백터값 움직임 이다. 저장 후 해보도록 하자. 움직여 보면 상당히 빠른걸 볼 수 있다. 

 

속도 조절

이제 속도를 조절하자.

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

public class palyer : MonoBehaviour
{
    public Rigidbody rbody;
    public Vector3 moveDir;
    public float moveSpeed;
    
    void Update()
    {
        moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); 
    }

    private void FixedUpdate()
    {
        rbody.MovePosition(rbody.position + moveDir * moveSpeed);
    }
}

새롭게 moveSpeed를 하나 만들어 준 다음에 곱하면 된다. 저장 후 유니티로 가보자.

 

그럼 Move Speed가 생기는데 0이면 당연히 안움직이고 숫자 값에 따라 속도가 조정된다.

 

nomarlize를 사용해 강제로 크기를 1로 만들어 주자.

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

public class palyer : MonoBehaviour
{
    public Rigidbody rbody;
    public Vector3 moveDir;
    public float moveSpeed;
    
    void Update()
    {
        moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
        moveDir.Normalize();
    }

    private void FixedUpdate()
    {
        rbody.MovePosition(rbody.position + moveDir * moveSpeed);
    }
}

어떻게 움직이든 속도가 일정하게 만들어 주는 것이다. 움직이면 속도가 일정하게 움직일 것이다.