반응형

유니티 2023.1.2.1에서 작업

유니티 에셋을 들어가서 Realistic Terrain Collection Lite  무료 에셋을 다운로드합니다 

window -> Package Manager 를 열고  임포트 합니다

 

임포트 한 파일에서 Terrain 5 파일 안에 자기가 맘에 드는 Terrain 파일 하나를 골라 Hierarchy 안으로 끄집어 올려놓습니다  

 

 

 

 

 

에셋 스토어에 다시 들어가서 Star Sparrow Modular Spaceship 에셋을 다운 받습니다 

 

Package Manager 에서 임포트 하여 다운로드합니다 

 

 

 

 

 

다운로드한 Star Sparrow 파일에서 Prefabs 폴더의 Examples에서 맘에 드는 비행기를 하나 골라서 Hierarchy 위에 끄집어 올려놓습니다 

 

올려놓은 비행기의 이름을 PlayerShip 으로 바꾸고 Assets폴더에 Prefabs 폴더를 하나 만들어서 드래그하여 프리팹으로 만듭니다 

 

 

 

 

 

 

 

오른쪽 마우스 버튼을 눌러서  Create Empty 오브젝트를 만든 다음 이름을 PlayerRig 로 바꾸고  Transform 값을 아래 그림과 같이 놓습니다  그리고 자식으로 PlayerRig와 Transform 값을 같은 값으로 하고 아래 그림과 같이 자식으로 놓습니다 

그리고 PlayerRig 자식으로 Main Camera 를 두고 Transform 값을 아래 그림과 같이 하여 카메라 보는 각도가 비행기 뒤쪽에서 바라보게 끔 합니다 

 

 

 

 

 

Create Empty 를 만들고 이름을 Master Timeline으로 합니다 

Master Timeline 선택하고 Window -> Sequencing -> Timeline 을 클릭하여 Timeline 창을 보이게 합니다 

 

Master Timeline 오브젝트를 선택하고  Timeline 창에 Create 버튼을 클릭하고 타임라인저장할 Assets 폴더에 새로운 폴더를 만듭니다 

 

Master Timeline 오브젝트를 선택하고 Timeline 창에서 +버튼을 누르고   Animation Track 를 선택합니다 

 Timeline 창에서 오른쪽의 자물쇠 모야을 잠금으로 합니다 

 

 

PlayerRig 오브젝트를 드래그 하여 Timeline 창의 Animation Track에 드래그합니다  

 

Timeline 창에서 아래와 같이 빨간 버튼을 누릅니다 

 

PlayerRig를 움직이면 Timeline 시간대에 점이 찍힘니다 PlayerRig를 자기가 움직이고 싶은 위치와 Timeline 시간대에 갔다 놓으면 됩니다  

 

Timeline의 아래와 같이 선 모양의 아이콘을 클릭하면 Animated Values 가 나오는데 찍혀있는 점들을 움직이면 위치와 회전을 바꿀 수 있습니다 

 

 

 

 

 

아래와 같이 PlayerRig 움직여 Timeline 시간대에 점이 찍히는 방향대로 움직인 결과 입니다 

 

 

Assets에 Scripts 폴더를 만들고 새로운 스크립트를 생성하여 이름을 PlayerControl을 만듭니다

 

 

 

 

 

스크립트를 작성합니다 

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using UnityEngine.InputSystem;
//using UnityEngine.Rendering.HighDefinition;

public class PlayerControl : MonoBehaviour
{   
    public float controlSpeed = 10f;
    public float xRange = 12f;
    public float yRange = 10f;
   

    public float positionPithcFactor = -2f;
    public float controlPithcfactor = -10f;
    public float positionYawFactor = 2f;

    public float controlRollFactor = -20f;
   

    float xThrow;
    float yThrow;
    void Update()
    {
        ProcessTranslation();
        ProcessRotation();    
    }

    //상하좌우로 움직일때 로컬 로테이션을 준다 
    void ProcessRotation()
    {
        //y축 로컬 움직임 팩터
        float pitchDueToPosition = transform.localPosition.y * positionPithcFactor;
        float pitchdueToControlThrow = yThrow * controlPithcfactor;


        float pitch = pitchDueToPosition + pitchdueToControlThrow;
        //x축 로컬 움직임 팩터
        float yaw = transform.localPosition.x * positionYawFactor;
        float roll = xThrow * controlRollFactor;

        //로컬축으로 로테이션한다
        transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
    }
    private void ProcessTranslation()
    {
        //좌우 입력
        xThrow = Input.GetAxis("Horizontal");
        //상하 입력
        yThrow = Input.GetAxis("Vertical");

        //시간에 따라 좌우 움직임을 준다 
        float xoffset = xThrow * Time.deltaTime * controlSpeed;
        float rawXPos = transform.localPosition.x + xoffset;

        //플레이어 좌우 이동 제한
        float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);

        //시간에 따라 상하 움직임을 준다 
        float yoffset = yThrow * Time.deltaTime * controlSpeed;
        float rawYpos = transform.localPosition.y + yoffset;

        //플레이어 상하 이동 제한
        float clampedYPos = Mathf.Clamp(rawYpos, -yRange, yRange);

        //키를 누르면 오브젝트를 상하좌우로 움직이게 한다 
        transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
    }
   
}

 

 

PlayerShip 오브젝트를 선택하고 PlayerControl 스크립트를 드래그 하여 붙입니다

그리고 아래그림과 같이 데이터 값을 넣습니다 

게임을 실행시켜서 AD(좌우) WS(상하) 키를 움직여서 비행기가 화면을 벋어나지 않고 잘 움직이는지 확인합니다  

 

 

 

반응형

+ Recent posts