1
0

UnityでSwitchのプロコンのジョイスティックを使う

Posted at

はじめに

Unityでプロコンのジョイスティックを使いたかった

やり方

Project Settingsをいじる

EditからProject Settingsを選択してInput Managerを選択
image.png

image.png
デフォルトだとSizeは18になっている.HorizontalとVerticalの部分を右クリックしてDuplicateArrayElementを選択する.図のようにわかりやすくLeftとRightをつけておく.
image.png

※ どのコントローラでもデフォルトでは右ジョイスティックが使えないのでこのように設定していく必要がある.

次にAxisという項目をいじる
以下のような番号に対応している.番号の割り当て謎過ぎる
image.png

スクリプトをいじる

PlayerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private float leftHorizontalAxis;
    private float leftVerticalAxis;
    private float rightHorizontalAxis;
    private float rightVerticalAxis;

    public float speed = 5.0f;

    private GameObject player;

    // Start is called before the first frame update
    void Start()
    {
        player = this.gameObject;
    }

    // Update is called once per frame
    void Update()
    {
        leftHorizontalAxis = Input.GetAxis("LeftHorizontal");
        leftVerticalAxis = Input.GetAxis("LeftVertical");
        rightHorizontalAxis = Input.GetAxis("RightHorizontal");
        rightVerticalAxis = Input.GetAxis("RightVertical");

        player.transform.position += new Vector3(leftHorizontalAxis, 0, leftVerticalAxis) * speed * Time.deltaTime;
        player.transform.position += new Vector3(rightHorizontalAxis, 0, rightVerticalAxis) * speed * Time.deltaTime;
    }
}

左右どちらのジョイスティックも同じ動きにしてる,めんどくさいので
適当に床と動かすオブジェクトを配置して実行する.動かす方にはスクリプトをアタッチするのを忘れないように
image.png

おわり

動かせたね,やったね

1
0
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
1
0