반응형

자동차의 시점을 따라가도록 카메라의 스크립트를 달아 자동차를 따라가게 하고 자동차의 도로 역할을 하는 사각 블록을 자동으로 생성되는 스크립트를 만들어보겠습니다

 

 

Hierarchy 뷰에 Create Empty 를 만들어서  이름을 PlatformSpawner로 합니다 

 

Scripts폴더에 PlatformSpawner 스크립트를 생성하고 스크립트작성할 준비를 합니다 

 

 

 

 

PlatformSpawner 오브젝트의 PlatformSpawner 스크립트를 붙힙니다 

 

 

 

PlatformSpawner 스크립트작성

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

public class PlatformSpawner : MonoBehaviour
{
    public GameObject platform;

    public Transform lastPlatform;
    Vector3 lastPosition;
    Vector3 newPos;


    bool stop;
    // Start is called before the first frame update
    void Start()
    {
        lastPosition = lastPlatform.position;

        StartCoroutine(SpawnPlatforms());

    }

    // Update is called once per frame
    void Update()
    {

  



        //if (Input.GetKey(KeyCode.Space))
        //{
        //    SpawnPlatforms();
        //}
    }

    IEnumerator SpawnPlatforms()
    {

        while (!stop)
        {
            GeneratePosition();

            //마지막플랫폼블럭위치에 newPos만큼 새로운 플랫폼블럭을 생성한다
            Instantiate(platform, newPos, Quaternion.identity);

            lastPosition = newPos;

            yield return new WaitForSeconds(0.1f);
        }


    }

    //void SpawnPlatforms()
    //{
    //    GeneratePosition();


    //    //마지막플랫폼블럭위치에 newPos만큼 새로운 플랫폼블럭을 생성한다
    //    Instantiate(platform, newPos, Quaternion.identity);
        
    //    lastPosition = newPos;
    //}

// 랜덤으로 lastPosition 값을 x,y 값 2만큼 움직이게 한다 
    void GeneratePosition()
    {
        newPos = lastPosition;

        int rand = Random.Range(0, 2);

        if (rand > 0)
        {
            newPos.x += 2f;
        }
        else
        {
            newPos.z += 2f;
        }

    }
}

 

 

 

 

 

Hierarchy뷰에서  PlatformSpawner 오브젝트를 선택하고 작성한 PlatformSpawner 스크립트 공백에 Platform 에는 프리 팹으로 넣은 PlatformP 프리 팹을 넣어주고, Last Platform 에는 박스 오브젝트 복사 모듈인 PlatformP (3)을 넣어줍니다

 

 

자동차의 움직임을 자연스럽게 카메라를 따라가는 스크립트를 만듭니다

Main Camera를 선택하고 CameraFollow 스크립트를 생성하여 Main Camera에 붙입니다 

 

 

 

CameraFollow 스크립트 작성

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

public class CameraFollow : MonoBehaviour
{
    public Transform target;

    Vector3 distance;
    public float smoothValu;

    // Start is called before the first frame update
    void Start()
    {
        // 카메라와 타겟 자동차와의 거리
        distance = target.position - transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        Follow();
    }

    void Follow()
    {
        //현재 카메라 포지션
        Vector3 currentPos = transform.position;

        //현재 타겟 자동차 
        Vector3 targetPos = target.position - distance;

        //currentPos(카메라)가 targetPos(자동차)을 smoothValu 만큼 time 프레임에 맞쳐 뒤따라간다
        transform.position =  Vector3.Lerp(currentPos, targetPos, smoothValu * Time.deltaTime);


    }
}

 

 

 

 

Main Camera를 선택하고  CameraFollow 스크립트 빈칸에 Target -> car1, smoothValu 5를 줍니다 

게임을 실행하여 자동차 움직임의 시점을 따라가는지 확인하고  자동차 도로의 블록이 자동으로 생성되는지 확인합니다

 

 

3D Game 자동차 게임 지그재그 게임만들기 4 로 이동

 

반응형

+ Recent posts