3
3

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でBluetooth接続したPS4のコントローラ(DUALSHOCK 4)を使う

Last updated at Posted at 2019-03-01

手元にあったBluetooth対応のコントローラがPS4のコントローラだったのでUnityで動くようにしてみました.
キーボード,USBゲームパッド,iPadを入力としてデモ展示することが多いのですが,今後はPS4コントローラでも良いかも.

とりあえずよく使うESP32やMirageSoloと組み合わせてみたい

2019/03/03追記
Android環境下ではInputManagerの割当が異なるため,Scriptを追加
本当は環境を認識して切り替えるのが良さそう
https://qiita.com/Ubermensch/items/75072ef89249cb3b30e7

#環境
Unity 2018.1.3f1
Windows10 Home 64bit
PS4 コントローラ CUH-ZCT2J
Bluetoothのペアリングはこのサイトを参考にしました(https://gacho-info.com/pc-ps4controller/)

#参考ページ
https://qiita.com/o_s_t/items/c18edeeee869d42c6eb9
https://qiita.com/nanmo/items/559a58f76dda82323283
https://gacho-info.com/pc-ps4controller/
https://www.jp.playstation.com/accessories/dualshock4/

#Unityプロジェクトはこちら(ダウンロードしたら使えると思います)
https://github.com/gupen/CheckPS4Controller

#InputManagerの設定
右スティックX軸 → X axis
右スティックY軸 → 3rd axis
左スティックX軸 → 4th axis
左スティックY軸 → 7th axis
R2トリガー → 6th axis
L2トリガー → 5th axis
十字キーX軸 → 8th axis
十字キーy軸 → 9th axis

私がBluetoothで接続してるからなのか,PCの設定によるものなのか分かりませんが,参考にさせて頂いたページ(https://qiita.com/o_s_t/items/c18edeeee869d42c6eb9 )と,私の環境では割当てが少し違う結果となりました.

動作確認用に単純にif文並べたコード↓

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CheckPS4Controller : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("joystick button 0"))
        {
            Debug.Log("□");
        }
        if (Input.GetKeyDown("joystick button 1"))
        {
            Debug.Log("✕");
        }
        if (Input.GetKeyDown("joystick button 2"))
        {
            Debug.Log("○");
        }
        if (Input.GetKeyDown("joystick button 3"))
        {
            Debug.Log("△");
        }
        if (Input.GetKeyDown("joystick button 4"))
        {
            Debug.Log("L1");
        }
        if (Input.GetKeyDown("joystick button 5"))
        {
            Debug.Log("R1");
        }
        if (Input.GetKeyDown("joystick button 6"))
        {
            Debug.Log("L2");
        }
        if (Input.GetKeyDown("joystick button 7"))
        {
            Debug.Log("R2");
        }
        if (Input.GetKeyDown("joystick button 8"))
        {
            Debug.Log("Share");
        }
        if (Input.GetKeyDown("joystick button 9"))
        {
            Debug.Log("Option");
        }
        if (Input.GetKeyDown("joystick button 10"))
        {
            Debug.Log("L3");
        }
        if (Input.GetKeyDown("joystick button 11"))
        {
            Debug.Log("R3");
        }
        if (Input.GetKeyDown("joystick button 12"))
        {
            Debug.Log("PS");
        }
        if (Input.GetKeyDown("joystick button 13"))
        {
            Debug.Log("TouchPad");
        }


        //Left Stick
        //1:→↓
        //-1:←↑
        float LeftStick_X = Input.GetAxis("LeftAnalogStick_X");
        Debug.Log("LeftAnalogStick_X:" + LeftStick_X );
        float LeftStick_Y = Input.GetAxis("LeftAnalogStick_Y");
        Debug.Log("LeftAnalogStick_Y:" + LeftStick_Y);

        //Right Stick
        //1:→↓
        //-1:←↑
        float RightStick_X = Input.GetAxis("RightAnalogStick_X");
        Debug.Log("RightAnalogStick_X:" + RightStick_X );
        float RightStick_Y = Input.GetAxis("RightAnalogStick_Y");
        Debug.Log("RightAnalogStick_Y:" + RightStick_Y);

        //L2|R2Trigger
        //1:Push
        //-1:Release
        float R2Trigger = Input.GetAxis("R2Trigger");
        Debug.Log("R2Trigger:" + R2Trigger);
        float L2Trigger = Input.GetAxis("L2Trigger");
        Debug.Log("L2Trigger:" + L2Trigger);


        //Dpad
        //1:↑→
        //-1:↓←
        float Dpad_X = Input.GetAxis("Dpad_X");
        Debug.Log("Dpad_X:" + Dpad_X);
        float Dpad_Y = Input.GetAxis("Dpad_Y");
        Debug.Log("Dpad_Y:" + Dpad_Y);

    }
}

#Android環境下ではInputManagerの割当が異なるため,Scriptを追加

Androidに繋いだときにボタンと動作が対応して無くて困ったが,下記サイトを参考に解決
https://nabesi777.hatenablog.com/entry/2018/10/04/Android%E7%AB%AF%E6%9C%AB%E3%81%A7PS%EF%BC%94%E3%81%AE%E3%82%B3%E3%83%B3%E3%83%88%E3%83%AD%E3%83%BC%E3%83%A9%E3%83%BC%E3%82%92%E4%BD%BF%E3%81%86%E9%9A%9B%E3%81%AEUnity%E3%83%9C%E3%82%BF%E3%83%B3

PC向けに設定したInputManagerを流用してAndroid対応しているので,AnalogInputの部分はInputManager名と一致してなくて汚い...


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CheckPS4Controller_Android : MonoBehaviour {

    private GameObject StatusText;
    private GameObject StatusText_Analog;
    private string StatusDigital;

    // Use this for initialization
    void Start()
    {
        StatusText = GameObject.Find("Status");
        StatusText_Analog = GameObject.Find("Status_Analog");
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("joystick button 0"))
        {
            StatusDigital = "□";
        }
        if (Input.GetKeyDown("joystick button 1"))
        {
            StatusDigital = "✕";
        }
        if (Input.GetKeyDown("joystick button 2"))
        {
            StatusDigital = "△";
        }
        if (Input.GetKeyDown("joystick button 3"))
        {
            StatusDigital = "L1";
        }
        if (Input.GetKeyDown("joystick button 4"))
        {
            StatusDigital = "L2";
        }
        if (Input.GetKeyDown("joystick button 5"))
        {
            StatusDigital = "R2";
        }
        if (Input.GetKeyDown("joystick button 6"))
        {
            StatusDigital = "Share";
        }
        if (Input.GetKeyDown("joystick button 7"))
        {
            StatusDigital = "Option";
        }
        if (Input.GetKeyDown("joystick button 8"))
        {
            StatusDigital = "TouchPad";
        }
        if (Input.GetKeyDown("joystick button 10"))
        {
            StatusDigital = "R3";
        }
        if (Input.GetKeyDown("joystick button 11"))
        {
            StatusDigital = "L3";
        }
        if (Input.GetKeyDown("joystick button 12"))
        {
            StatusDigital = "PS";
        }
        if (Input.GetKeyDown("joystick button 13"))
        {
            StatusDigital = "○";
        }
        if (Input.GetKeyDown("joystick button 14"))
        {
            StatusDigital = "R1";
        }

        StatusText.GetComponent<TextMesh>().text = StatusDigital;


        //Left Stick
        //1:→↓
        //-1:←↑
        float LeftStick_X = Input.GetAxis("LeftAnalogStick_X");
        float LeftStick_Y = Input.GetAxis("A");

        //Right Stick
        //1:→↓
        //-1:←↑
        float RightStick_X = Input.GetAxis("B");
        float RightStick_Y = Input.GetAxis("C");

        //L2|R2Trigger
        //1:Push
        //-1:Release
        float R2Trigger = Input.GetAxis("RightAnalogStick_X");
        float L2Trigger = Input.GetAxis("LeftAnalogStick_Y");


        //Dpad
        //1:↑→
        //-1:↓←
        float Dpad_X = Input.GetAxis("L2Trigger");
        float Dpad_Y = Input.GetAxis("R2Trigger");

        StatusText_Analog.GetComponent<TextMesh>().text =
            "LeftAnalogStick_X:" + LeftStick_X + "\n"
            + "LeftAnalogStick_Y:" + LeftStick_Y + "\n"
            + "RightAnalogStick_X:" + RightStick_X + "\n"
            + "RightAnalogStick_Y:" + RightStick_Y + "\n"
            + "R2Trigger:" + R2Trigger + "\n"
            + "L2Trigger:" + L2Trigger + "\n"
            + "Dpad_X:" + Dpad_X + "\n"
            + "Dpad_Y:" + Dpad_Y;

    }
}

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?