상세 컨텐츠

본문 제목

유니티엔진으로 만든 2D게임 분석하기_2

카테고리 없음

by 김일국 2020. 12. 5. 15:11

본문

지난 포스트에 이이서, 오늘은 게임오브젝트를 키보드로 제어해서 이동시키는 작업을 해 보았습니다.

계증구조(하이어러키) > 2D 오브젝트 > 스프라이트에 적용된 스크립트 C#소스는 아래와 같습니다.

내용은 인프런 강의 [따라하면서 배우는 고박사의 유니티 기초] 를 참조 했습니다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement2D : MonoBehaviour
{
    private float moveSpeed = 5.0f;//이동속도
    private Vector3 moveDirection = Vector3.zero;//이동방향
    /*private void Awake()
    {
        //새로운위치 = 현재위치+(방향*속도)
        transform.position = transform.position + new Vector3(1, 0, 0) * 1;
        //transform.position+=Vector3.right*1;
    }
    // Start is called before the first frame update
    void Start()
    {
    }*/
    // Update is called once per frame
    void Update()
    {
        //단축키로 게임캐릭터 이동시키기
        //Horizontal: left(Negative=-1), right(Positive=+1)
        //Vertical: down(Negative=-1), up(Positive=+1)
        float x = Input.GetAxisRaw("Horizontal");//좌우
        float y = Input.GetAxisRaw("Vertical");//상하
        //이동방향 설정
        moveDirection = new Vector3(x, y, 0);
        //새로운위치=현재위치+(방향*속도)
        transform.position+=moveDirection*moveSpeed*Time.deltaTime;
    }
}

 

신기하고 재이 있네요^^

참고로 게임 화면에 추가한 캐릭터가 나타나지 않아서 당황했었는데요...아래처럼 해서 해결^^

계증(하이어라키) 탭 에서 메인카메라를 선택 후 Ctrl+Shift+F 를 누르면 현재 Scene에서 보이는 화면이 게임씬에서도 똑같이 나오게 되는것을 확인 할 수 있었습니다.(기술참조: hyunity3d.tistory.com/416 )

댓글 영역