LoginSignup
8
8

More than 5 years have passed since last update.

【Unity】Mobile JoystickアセットをC#で使う

Posted at

Unityのstandard assetsにモバイル用のJoyStickがあり便利ですが、これJavascriptで書かれてるのでC#派の人にはちょっと不便。
kobito.1403411337.423777.png

そこで調べてみると、これをC#に移植してくれてる方がいました。
http://wiki.unity3d.com/index.php?title=Joystick

英語なので手順を日本語にして残しておきます。

C#スクリプトを用意

適当な場所にC#スクリプトを作って、上記URLのスクリプトをコピー。

例では下記に置いた。
Standard Assets(Mobile) > Scripts

kobito.1403410794.786658.png

C#スクリプトをアタッチ

もとのJavaScriptの代わりに、いま作成したC#スクリプトをアタッチ。
kobito.1403410909.273049.png

Joystickに「joystick」というタグを付けておく

これ大事!これをしていないとExceptionエラーが出る。
新規にjoystickというタグを作りました。
kobito.1403411040.952676.png

コード例

using UnityEngine;
using System.Collections;

public class GunController : MonoBehaviour {

    public Joystick joystick; // joystickオブジェクトはインスペクタから指定しました

    // Update is called once per frame
    void Update () {

        if (joystick.position.x > 0) {
            transform.Rotate(0, 1, 0);
        } else if (joystick.position.x < 0) {
            transform.Rotate(0, -1, 0);
        }

    }
}

8
8
1

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
8
8