반응형

이번에는 자동차가 도로에서 벗어나면 자동차가 떨어지고 게임이 끝나는 시연을 하겠습니다

 

먼저 Hierarchy뷰에서 car1을 선택하고 is Kinematic을 체크 해저 합니다 

is Kinematic을 해제하면 자동차가 중력의 영향을 받게 됩니다 

 

CarCtrl스크립트를 수정합니다 

 

Update() 함수에

     if (transform.position.y <= -2)
        {
            GameManager.instance.GameOver();
        }

항목을 추가합니다 

car1 이 중력을 받고 떨어지면 게임을 끝내는 GameOver() 함수를 실행합니다 

 

 

 

 

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

public class CarCtrl : MonoBehaviour
{
    public float moveSpeed;
    bool sideMoving = true;
    bool firstInput = true;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (GameManager.instance.gameStarted)
        {
            Move();
            CheckInput();
        }

        //자동차가 중력을 받고 떨어졌을때 y 값이 떨어지면 게임오버실행
        if (transform.position.y <= -2)
        {
            GameManager.instance.GameOver();
        }
    }

    void Move()
    {
        //물체의 z축 방향(forward)으로 moveSpeed 만큼 시간 프레임에 곱해준다 
        transform.position += transform.forward * moveSpeed * Time.deltaTime;
    }


    void CheckInput()
    {
        //첫 클릭은 무시하고 리턴한다 
        if (firstInput)
        {
            firstInput = false;
            return;
        }


        //마우스를 왼쪽버튼 클릭하면
        if (Input.GetMouseButtonDown(0))
        {
            ChangeDirection();
        }
    }
    void ChangeDirection()
    {
        //sideMoving 참일때  
        if (sideMoving)
        {
            sideMoving = false;

            //y축 방향으로 90도 회전한다
            transform.rotation = Quaternion.Euler(0, 90, 0);
        }
        else
        {
            sideMoving = true;
            transform.rotation = Quaternion.Euler(0, 0, 0);
        }
    }
}

 

 

 

GameManager스크립트를 수정합니다 

 

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

public class GameManager : MonoBehaviour
{
    public static GameManager instance;

    //
    public GameObject platformSpawner;

    public bool gameStarted;

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (!gameStarted)
        {
            //마우스 클릭하면 차가 움직인다 
            if (Input.GetMouseButtonDown(0))
            {
                GameStart();
            }
        }
    }

    public void GameStart()
    {
        gameStarted = true;
        //platformSpawner 스크립트를 실행
        platformSpawner.SetActive(true);
    }


    public void GameOver()
    {
        //platformSpawner 스크립트를 없앤다
        platformSpawner.SetActive(false);
    }
}

 

GameManager 스크립트를 수정하고 GameManager스크립트 PlatformSpawner 빈 공간에 PlatformSpawner 오브젝트를 연결합니다 

 

 

 

Hierarchy뷰에서 PlatformSpawner 오브젝트를 선택하고 Inspector를 체크 해제합니다 

 

Main Camera를 선택하여 CameraFollow 스크립트를 수정합니다 

 

 

Update() 함수 부분을 수정하여 

      if (target.position.y >= 0)
        {
            Follow();
        }

 

자동차가 y값이 0 이상일 때만 따라 움직이게 합니다 

 

 

 

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()실행, 떨어지면 카메라가 움직이지 않는다 
        if (target.position.y >= 0)
        {
            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);


    }
}

 

 

 

 

Platform 스크립트를 생성합니다 

 

Prefab 폴더의 PlatformP 프리 팹을  선택하고 Plaform 스크립트를 연결합니다

 

 

 

Platform 스크립트 작성

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

public class Platform : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        

    }

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

    private void OnCollisionExit(Collision collision)
    {

        //Player태그를 가진 오브젝트와 닿았을때  0.2초후 Fall()실행
        if (collision.gameObject.tag == "Player")
        {
            Invoke("Fall", 0.2f);
        }
    }
    void Fall()
    {
        //중력의 영향을 받고 물체를 떨어진다  그리고 1초후 삭제 
        GetComponent<Rigidbody>().isKinematic = false;
        Destroy(gameObject, 1f);
    }
}

 

 

 

 

car1 오브젝트를 선택하고 Inspector에서 Tag -> Player를 줍니다 

 

 

게임을 실행하여 도로블록이 자동차가 지나갔을 때 아래로 떨어지고 자동차가 도로에서 벗어나면 자동차가 중력을 받고 떨어지고 블록 생성이 중단되는지 확인합니다 

 

반응형

+ Recent posts