본문 바로가기
Unity/수업내용

[Unity 2D / 유니티 교과서] 4장 예제 SwipeCar

by 왹져박사 2023. 1. 30.
728x90

AudioSource mp3파일이 아닌 ogg파일로 변경해서 사용하기! ( 용량 최소 약 1/5로 줄음)

Input.mousePosition Unity공간 좌표가 아니라 실행화면의 Screen(픽셀공간) 좌표

 죄표계는 3D, Screen, UI 각각 존재

Awake 이벤트 함수. (활성화된 오브젝트에서!)인스턴스 생성 직후 실행. Start보다 먼저 실행

transform.Translate(x, y, z, Space.self/world)

로컬 좌표. 월드 좌표GameObject.Find("string") Scene안에 있는 GameObject 이름 검색(오타 주의)

 

CarController

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

public class CarController : MonoBehaviour
{
    float speed = 0;
    private Vector2 startPos;

    //이벤트 함수
    //인스턴스화 될 때 한번만 호출(Start전에 호출됨)
    private void Awake()
    {
        
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
        if (Input.GetMouseButtonDown(0))
        {
            //Debug.Log("이동");
            //this.speed = 0.2f;
            //Debug.Log(Input.mousePosition);
            this.startPos = Input.mousePosition;
        }
        else if (Input.GetMouseButtonUp(0))
        {
            //Debug.LogFormat("start : {0}", Input.mousePosition);
            Vector2 endPos = Input.mousePosition;
            //Debug.LogFormat("end : {0}", Input.mousePosition);
            float len = endPos.x - this.startPos.x;

            this.speed = len / 800.0f;

            GetComponent<AudioSource>().Play();
        }
        
        this.transform.Translate(speed, 0, 0);
        speed *= 0.98f;
        
    }
}

GameDirector

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

public class GameDirector : MonoBehaviour
{
    private GameObject car;
    private GameObject flag;
    private GameObject distance;
    // Start is called before the first frame update
    void Start()
    {
        this.car = GameObject.Find("car");
        this.flag = GameObject.Find("flag");
        this.distance = GameObject.Find("Distance");

        //Debug.LogFormat("car : {0}", this.car);
        //Debug.LogFormat("flag : {0}", this.flag);
        //Debug.LogFormat("distance : {0}", this.distance);
    }

    // Update is called once per frame
    void Update()
    {
        //var a = this.car.transform.position;
        //var b = this.flag.transform.position;
        //두 점 사이의 거리를 계산
        //var len = Vector2.Distance(a, b);
        //Debug.LogFormat("목표 지점까지 {0}", len);
        //Text texDistance = this.distance.GetComponent<Text>();
        //texDistance.text = string.Format("목표 지점까지 {0:F2}m", len);
        //this.distance.GetComponent<Text>().text = "목표 지점까지 " + len.ToString("F2") + "m";

        var ax = this.car.transform.position.x;
        var bx = this.flag.transform.position.x;
        var cx = bx - ax;
        Text texDistance = this.distance.GetComponent<Text>();
        if (cx >= 0)
        {
            texDistance.text = string.Format("목표 지점까지 {0:F2}m", cx);
        }
        else
        {
            texDistance.text = string.Format("Gameover");
        }
    }
}

 

728x90