본문 바로가기
유니티기초

2D 플랫포머 게임 만들기 2탄~

by unity_dpipe 2023. 3. 23.
반응형

C#을 통한 캐릭터 제어 및 애니메이션 컨트롤

2D 플랫포머게임을 만들기 위한 가이드 영상입니다. C# 스크립트를 활용하여 키보드 입력 값에 따라 캐릭터가 달리고 점프합니다.  변수와 if 조건문을 활용하여 애니이션을 컨트롤하는 과정을 통해 캐릭터가 뛰는 동작을  구현할 수 있습니다. 수업 가이드 영상으로 나레이션은 생략되었습니다.

 

사용된 코드는 다음과 같습니다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController2 : MonoBehaviour
{
    private Animator animator;
    private float speed = 0.03f;
    void Start(){
        animator = GetComponent<Animator>();
    }
    void Update()
    {
        Debug.Log("Horizontal: " +Input.GetAxisRaw("Horizontal"));
        Debug.Log("Vertical: " +Input.GetAxisRaw("Vertical"));
        if(Input.GetAxisRaw("Horizontal") < 0 || Input.GetAxisRaw("Horizontal") > 0){
            transform.Translate(speed * Input.GetAxisRaw("Horizontal"), 0, 0);
            transform.localScale = new Vector2(Input.GetAxisRaw("Horizontal"), 1);
            animator.SetBool("Run", true);
        } else if(Input.GetAxisRaw("Horizontal") == 0){
            animator.SetBool("Run", false);
        }
        if(Input.GetAxisRaw("Vertical") > 0){
            Debug.Log("jump");
             transform.Translate(0, speed, 0);
            animator.SetBool("Jump", true);
        } else if(Input.GetAxisRaw("Vertical") == 0) {
            animator.SetBool("Jump", false);
        }
    }
}

 

2D 플랫포머 게임 만들기 2탄~

https://www.youtube.com/watch?v=luT8GF5QrBs&t=2s 

 

반응형