// 增加紀錄左鍵的次數&顯示 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class CarController : MonoBehaviour { GameObject Lefttxt; GameObject distance; float speed = 0; public static int left_key = 0; float right_key = 0; Vector2 startPos; // Start is called before the first frame update void Start() { Lefttxt= GameObject.Find("Lefttxt"); //this.distance = GameObject.Find("Distance"); } // Update is called once per frame void Update() { if(Input.GetMouseButtonDown(1)) { // 滑鼠右鍵 this.speed = 0.2f; this.right_key +=1.0f; } if(Input.GetMouseButtonDown(0)) { // 滑鼠左鍵 this.speed = -0.2f; left_key = left_key + 1; Lefttxt.GetComponent().text = "Left = "+left_key.ToString("D5")+"次"; } //取得滑動長度 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 / 500f; //播放音效 GetComponent().Play(); } transform.Translate(this.speed,0,0); this.speed *= 0.98f; } }