マウスをドラッグしてプレイヤーを動かす

今日はマウスをドラッグするとプレイヤーが動くようにする

参考元は

Unityの教科書 Unity 2019完全対応版 

2D&3Dスマートフォンゲーム入門講座 Kindle

ここに乗っていたプログラムだ。

 

  1. float speed = 0;
  2. Vector3 startPos;
  3. // Start is called before the first frame update
  4. void Start()
  5. {
  6. }
  7. // Update is called once per frame
  8. void Update()
  9. {
  10. if(Input.GetMouseButtonDown(0))
  11. {
  12. this.startPos = Input.mousePosition;
  13. }
  14. else if(Input.GetMouseButtonUp(0))
  15. {
  16. Vector3 endPos = Input.mousePosition;
  17. float swipelength = endPos.x - startPos.x;
  18. this.speed = swipelength / 5000.0f;
  19. if(this.speed>=0.1f)
  20. {
  21. this.speed = 0.1f;
  22. }
  23. if (this.speed <= -0.1f)
  24. {
  25. this.speed = -0.1f;
  26. }
  27. Debug.Log(speed);
  28. }
  29. transform.Translate(this.speed,0, 0);
  30. this.speed *= 0.99f;
  31. }

 

少し内容を変更して速度制限を付けた。

これによってプレイヤーを移動させる。

 

これを利用してcantンなゲームを作った。

マウスでパドルを動かしてボールを跳ね返すゲームだ。

上の青い壁でポイントになり、落とすとポイントが減る。

後はタイマー機能を付けて公開したい。

f:id:sashimimayonezu:20200814225406p:plain

 

 

https://help.hatenablog.com/entry/developer-option?_gl=1*ojnx55*_gcl_au*NzA5NzY1Mzc3LjE3MTA2NTgyODA.