今日はマウスをドラッグするとプレイヤーが動くようにする
参考元は
Unityの教科書 Unity 2019完全対応版
ここに乗っていたプログラムだ。
- float speed = 0;
- Vector3 startPos;
- // Start is called before the first frame update
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- if(Input.GetMouseButtonDown(0))
- {
- this.startPos = Input.mousePosition;
- }
- else if(Input.GetMouseButtonUp(0))
- {
- Vector3 endPos = Input.mousePosition;
- float swipelength = endPos.x - startPos.x;
- this.speed = swipelength / 5000.0f;
- if(this.speed>=0.1f)
- {
- this.speed = 0.1f;
- }
- if (this.speed <= -0.1f)
- {
- this.speed = -0.1f;
- }
- Debug.Log(speed);
- }
- transform.Translate(this.speed,0, 0);
- this.speed *= 0.99f;
- }
少し内容を変更して速度制限を付けた。
これによってプレイヤーを移動させる。
これを利用してcantンなゲームを作った。
マウスでパドルを動かしてボールを跳ね返すゲームだ。
上の青い壁でポイントになり、落とすとポイントが減る。
後はタイマー機能を付けて公開したい。