4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

unityでswipeさせる

Posted at

背景

シューティングゲームを作っていて、スワイプで自機を移動させたかったので実現方法をメモ

前提

物体を移動させるには下記の2種類の方法があるが、今回はrigidbodyを使うやり方でやってみた。

  • transform.position(座標)を上書きする方法
  • rigidbodyを使ってrigidbody.velocityに速度ベクトルを代入する方法

コード

PlayerController.cs
    void Update()
    {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
            deltaPosition = Input.GetTouch(0).deltaPosition / 100; // 100で割ってるのは割らないと移動距離が長くなりすぎるので小さくするため
            rigidbody.velocity = new Vector3(deltaPosition.x, 0f, deltaPosition.y) * speed;
        }

備考

unity超初心者なので変な箇所あったら教えてほしいです。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?