引き続きUnityでアプリを作成しました。
今回はチキンレースを題材にしたものです。
コードの誤入力(「;」忘れやoとpの打ち間違え)一箇所でアプリ全体が動かなくなるのは厳しい注文ですが、慣れればミスも減ると思うので継続して作成していきます。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarController : MonoBehaviour
{
float speed =0;
Vector2 startPos;
void Start()
{
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
this.startPos=Input.mousePosition;
}
else if(Input.GetMouseButtonUp(0))
{
Vector2 endPos=Input.mousePosition;
float swipeLength=endPos.x - this.startPos.x;
this.speed=swipeLength/500.0f;
GetComponent<AudioSource>().Play();
}
transform.Translate(this.speed,0,0);
this.speed *= 0.98f;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour
{
GameObject car;
GameObject flag;
GameObject distance;
void Start()
{
this.car=GameObject.Find("car");
this.flag=GameObject.Find("flag");
this.distance=GameObject.Find("Distance");
}
void Update()
{
float length = this.flag.transform.position.x - this.car.transform.position.x;
if(length>=0)
{
this.distance.GetComponent<Text>().text= "ゴールまで" + length.ToString("F2") + "m";
}
else
{
this.distance.GetComponent<Text>().text="ゲームオーバー";
}
}
}