19
22

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] シューティングゲームに使える相対移動を実装する

Last updated at Posted at 2015-09-26

スマートフォンでシューティングゲームの自機を操作する場合、バーチャルパッドよりも相対移動の方が相性が良いです。
相対移動とは、画面上で任意の場所をタッチしてから指をスライドすることで、その移動分だけ自機が移動するような操作を指します。

メインカメラの設定により実装方法が異なりますので、それぞれまとめておきます。
それぞれのスクリプトを適当なオブジェクト(Cubeとか)にそのまま追加すればすぐに動きます。

##メインカメラのProjection設定がOrthographicの場合

using UnityEngine;
using System.Collections;

public class player : MonoBehaviour {

    private Vector3 playerPos;
    private Vector3 mousePos;

    void Update () {
        playerControl ();
    }

    private void playerControl(){

        if (Input.GetMouseButtonDown (0)) {
            playerPos = this.transform.position;
            mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }

        if (Input.GetMouseButton (0)) {

            Vector3 prePos = this.transform.position;
            Vector3 diff = Camera.main.ScreenToWorldPoint (Input.mousePosition) - mousePos;

            //タッチ対応デバイス向け、1本目の指にのみ反応
            if (Input.touchSupported) {
                diff = Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position) - mousePos;
            }

            diff.z = 0.0f;
            this.transform.position = playerPos + diff;

        }

        if (Input.GetMouseButtonUp (0)) {
            playerPos = Vector3.zero;
            mousePos = Vector3.zero;
        }
    }
}

##メインカメラのProjection設定がPerspectiveの場合
メインカメラがz=-10に、自機がz=0にいる前提です。
それ以外の場合は、適宜adjustmentの数値を調整してください。

using UnityEngine;
using System.Collections;

public class player : MonoBehaviour {

    public float adjustment = 3.0f;

    private Vector3 playerPos;
    private Vector3 mousePos;

    void Update () {
        playerControl ();
    }

    private void playerControl(){

        if (Input.GetMouseButtonDown (0)) {
            playerPos = this.transform.position;
            mousePos = Camera.main.ScreenToViewportPoint(Input.mousePosition) * Camera.main.fieldOfView / adjustment;
        }

        if (Input.GetMouseButton (0)) {

            Vector3 prePos = this.transform.position;
            Vector3 diff = Camera.main.ScreenToViewportPoint (Input.mousePosition) * Camera.main.fieldOfView / adjustment - mousePos;

            //タッチ対応デバイス向け、1本目の指にのみ反応
            if (Input.touchSupported) {
                diff = Camera.main.ScreenToViewportPoint (Input.GetTouch (0).position) * Camera.main.fieldOfView / adjustment - mousePos;
            }

            diff.z = 0.0f;
            this.transform.position = playerPos + diff;

        }

        if (Input.GetMouseButtonUp (0)) {
            playerPos = Vector3.zero;
            mousePos = Vector3.zero;
        }
    }
}
19
22
2

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
19
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?