아카이브

[개발일지] Undead Survivor 1일차 본문

2D 콘텐츠 제작

[개발일지] Undead Survivor 1일차

timbercat 2023. 9. 13. 18:15

플레이어 캐릭터 이동까지 구현

Rigidbody2D, Capsule Collider 2D 컴포넌트 부착 그래비티 스케일과 사이즈를 설정해준다

Player.cs

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

public class Player : MonoBehaviour
{
    public Vector2 inputVec;
    public float speed;
    Rigidbody2D rbody;

    // Awake는 시작할 때 한번만 실행되는 생명주기
    void Awake()
    {
        rbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        //GetAxisRaw로 더욱 명확한 컨트롤 구현 가능
        inputVec.x = Input.GetAxisRaw("Horizontal");
        inputVec.y = Input.GetAxisRaw("Vertical");
    }

    //FixedUpdate: 물리 연산 프레임마다 호출되는 생명주기 함수
    void FixedUpdate()
    {
        //위치 이동
        Vector2 nextVec = inputVec.normalized * speed * Time.fixedDeltaTime;
        //MovePosition은 위치이동이라 현재위치도 더해주어야 함
        rbody.MovePosition(rbody.position + nextVec ); 
    }
}

Input System 사용

패키지 매니저에서 Input System 설치

인풋 액션 설정하기

Player Input 컴포넌트 추가

Creat Actions 선택해 에셋 생성

Move 세팅

스크립트 적용

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

public class Player : MonoBehaviour
{
    public Vector2 inputVec;
    public float speed;
    Rigidbody2D rbody;

    // Awake는 시작할 때 한번만 실행되는 생명주기
    void Awake()
    {
        rbody = GetComponent<Rigidbody2D>();
    }

    //FixedUpdate: 물리 연산 프레임마다 호출되는 생명주기 함수
    void FixedUpdate()
    {
        //위치 이동
        Vector2 nextVec = inputVec * speed * Time.fixedDeltaTime;
        //MovePosition은 위치이동이라 현재위치도 더해주어야 함
        rbody.MovePosition(rbody.position + nextVec ); 
    }

    void OnMove(InputValue value)
    {
        inputVec = value.Get<Vector2>();
    }
}

방향 바라보기

Flip의 X를 누를 때 캐릭터가 바라보는 방향이 바뀐다

좌측 방향키를 눌렀을 때 방향을 바꿔 바라보아야 함으로 X값이 -1 일 때 체크되어야 함

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

public class Player : MonoBehaviour
{
    public Vector2 inputVec;
    public float speed;
    Rigidbody2D rbody;
    SpriteRenderer spriter;

    // Awake는 시작할 때 한번만 실행되는 생명주기
    void Awake()
    {
        rbody = GetComponent<Rigidbody2D>();
        spriter = GetComponent<SpriteRenderer>();
    }

    //FixedUpdate: 물리 연산 프레임마다 호출되는 생명주기 함수
    void FixedUpdate()
    {
        //위치 이동
        Vector2 nextVec = inputVec * speed * Time.fixedDeltaTime;
        //MovePosition은 위치이동이라 현재위치도 더해주어야 함
        rbody.MovePosition(rbody.position + nextVec ); 
    }

    void OnMove(InputValue value)
    {
        inputVec = value.Get<Vector2>();
    }

    //LateUpdate: 프레임이 종료되기 전 실행되는 생명주기 함수
    void LateUpdate()
    {
        if (inputVec.x != 0)
        {
            spriter.flipX = inputVec.x < 0; //inputVec.x의 값이 0보다 작을 땐 true 클 땐 false 값이 들어감
        }
    }
}

애니메이션

뜯어논 스프라이트 파일로 애니메이션 만들어줌

Dead를 트리거로 설정, 루프타임 해제
Speed 값이 0.01보다 크면 뛰는 모션으로 넘어감, 작으면 다시 서있는 모션으로 넘어감

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

public class Player : MonoBehaviour
{
    public Vector2 inputVec;
    public float speed;
    Rigidbody2D rbody;
    SpriteRenderer spriter;
    Animator anim;

    // Awake는 시작할 때 한번만 실행되는 생명주기
    void Awake()
    {
        rbody = GetComponent<Rigidbody2D>();
        spriter = GetComponent<SpriteRenderer>();
        anim = GetComponent<Animator>();
    }

    //FixedUpdate: 물리 연산 프레임마다 호출되는 생명주기 함수
    void FixedUpdate()
    {
        //위치 이동
        Vector2 nextVec = inputVec * speed * Time.fixedDeltaTime;
        //MovePosition은 위치이동이라 현재위치도 더해주어야 함
        rbody.MovePosition(rbody.position + nextVec ); 
    }

    void OnMove(InputValue value)
    {
        inputVec = value.Get<Vector2>();
    }

    //LateUpdate: 프레임이 종료되기 전 실행되는 생명주기 함수
    void LateUpdate()
    {
        anim.SetFloat("Speed", inputVec.magnitude); //magnitude: 백터의 순수 크기 값

        if (inputVec.x != 0)
        {
            spriter.flipX = inputVec.x < 0; //inputVec.x의 값이 0보다 작을 땐 true 클 땐 false 값이 들어감
        }
    }
}

새로운 캐릭터로 애니메이션 만들기

애니메이션 오버라이드 컨트롤러를 사용해서 처음에 만들어 놓은 애니메이터를 복사해 사용할 수 있음
플레이어 복사해서 새로 만든 다음 애니메이터 컨트롤러만 위에서 만든 Player1로 바꿔줌