C# ゲーム 2D 移動 スワイプの強さに合わせて X軸移動 & 効果音
using UnityEngine;
using System.Collections;
public class CarController : MonoBehaviour {
float speed = 0;
Vector2 startPos;
// Use this for initialization
void Start () {
}
// Update is called once per frame
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); // x軸の移動
this.speed *= 0.97f; // 減速
}
}