質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

873閲覧

joystickの動作について

tanakatarou2

総合スコア6

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/04/09 07:57

編集2020/04/09 08:15

unity 初心者です。アセットのJoystickでThirdPersonを動作についての質問になります。
現状Joystickにて動作はしますが、入力方向と違う方向に動作してしまいます。
上を入力で右へ右入力で下に動作していまい、入力方向と90度ずれた方面に進んでしまいます。
どの部分修正をしたらいいのかご教授いただければ幸いです。

下記コードが自分で修正を加えたscriptになります。

利用したアセット;Joystick Pack/Standard Assets (for Unity 2017.3)
参考にした動画:https://www.youtube.com/watch?v=8ycgJbQegAo

C#

1 2 3using System.Collections; 4using System.Collections.Generic; 5using UnityEngine; 6using UnityStandardAssets.Characters.ThirdPerson; 7 8public class MyController : MonoBehaviour 9{ 10 public FixedJoystick LeftJostick; 11 public FixedButton Button; 12 protected ThirdPersonUserControl control; 13 14 15 16 void Start() 17 { 18 control = GetComponent<ThirdPersonUserControl>(); 19 } 20 21 // Update is called once per frame 22 void Update() 23 { 24 control.m_Jump = Button.Pressed; 25 control.Hinput = LeftJostick.Horizontal; 26 control.Vinput = LeftJostick.Vertical; 27 } 28}

C#

1using System; 2using UnityEngine; 3using UnityStandardAssets.CrossPlatformInput; 4 5namespace UnityStandardAssets.Characters.ThirdPerson 6{ 7 [RequireComponent(typeof (ThirdPersonCharacter))] 8 public class ThirdPersonUserControl : MonoBehaviour 9 { 10 private ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object 11 private Transform m_Cam; // A reference to the main camera in the scenes transform 12 private Vector3 m_CamForward; // The current forward direction of the camera 13 private Vector3 m_Move; 14 [HideInInspector] 15 public bool m_Jump; // the world-relative desired move direction, calculated from the camForward and user input. 16 [HideInInspector] 17 public float Hinput; 18 [HideInInspector] 19 public float Vinput; 20 21 private void Start() 22 { 23 // get the transform of the main camera 24 if (Camera.main != null) 25 { 26 m_Cam = Camera.main.transform; 27 } 28 else 29 { 30 Debug.LogWarning( 31 "Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject); 32 // we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them! 33 } 34 35 // get the third person character ( this should never be null due to require component ) 36 m_Character = GetComponent<ThirdPersonCharacter>(); 37 } 38 39 40 private void Update() 41 { 42 if (!m_Jump) 43 { 44 m_Jump = CrossPlatformInputManager.GetButtonDown("Jump"); 45 } 46 } 47 48 49 // Fixed update is called in sync with physics 50 private void FixedUpdate() 51 { 52 // read inputs 53 //float h = CrossPlatformInputManager.GetAxis("Horizontal"); 54 //float v = CrossPlatformInputManager.GetAxis("Vertical"); 55 bool crouch = Input.GetKey(KeyCode.C); 56 57 // calculate move direction to pass to character 58 if (m_Cam != null) 59 { 60 // calculate camera relative direction to move: 61 m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized; 62 m_Move =Vinput *m_CamForward +Hinput *m_Cam.right; 63 } 64 else 65 { 66 // we use world-relative directions in the case of no main camera 67 m_Move = Vinput * Vector3.forward + Hinput * Vector3.right; 68 } 69#if !MOBILE_INPUT 70 // walk speed multiplier 71 if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 0.5f; 72#endif 73 74 // pass all parameters to the character control script 75 m_Character.Move(m_Move, crouch, m_Jump); 76 m_Jump = false; 77 } 78 } 79} 80

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2020/04/09 07:59

違う方向とはどういう方向なんでしょうか。 質問を編集して、そこらへんの詳しい説明を追記しましょう。
guest

回答1

0

ベストアンサー

上を入力で右へ移動するなら、
Vinputにm_CamForwardではなくm_CamRightをかければ上へ移動するようになります。

右を入力で下へ移動するなら、
Hinputにm_CamRightではなくm_CamForwardをかければ左へ移動するようになるので、
それにマイナスをかければ右へ移動するようになります。

c#

1 // calculate move direction to pass to character 2 if (m_Cam != null) 3 { 4 // calculate camera relative direction to move: 5 m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized; 6 Vector3 m_CamRight = Vector3.Scale(m_Cam.right, new Vector3(1, 0, 1)).normalized; 7 m_Move =Vinput *m_CamRight -Hinput *m_CamForward; 8 }

投稿2020/04/09 09:26

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問