CarController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarController : MonoBehaviour
{
[SerializeField]
float speed = 0;
Vector2 startPos;
private void Awake()
{
Debug.Log("Awake");
}
// Start is called before the first frame update
void Start()
{
Debug.Log("start");
}
// Update is called once per frame
void Update()
{
// 버튼을 누르면
if (Input.GetMouseButtonDown(0))
{
Debug.Log("down");
this.startPos = Input.mousePosition;
Debug.LogFormat("startPos: {0}", this.startPos);
this.speed = 0.2f;
}
else if (Input.GetMouseButtonUp(0))
{
Debug.Log("Up");
Vector2 endPos = Input.mousePosition;
Debug.LogFormat("endPos: {0}", endPos);
float length = endPos.x - this.startPos.x;
Debug.LogFormat("length: {0}", length);
this.speed = length / 500f;
Debug.LogFormat("speed: {0}", this.speed);
this.GetComponent<AudioSource>().Play();
//AudioSource audio = this.GetComponent<AudioSource>();
//audio.Play();
}
// 이동
this.transform.Translate(this.speed, 0, 0);
// 감속
this.speed *= 0.96f;
}
}
GameDirector.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour
{
public GameObject car;
[SerializeField]
private GameObject flag;
[SerializeField]
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()
{
float length = this.flag.transform.position.x - this.car.transform.position.x;
Text textDistance = this.distance.GetComponent<Text>();
textDistance.text = string.Format("목표 지점까지 {0}m", length.ToString("F2"));
}
}