お世話になっております。
Unity2017.3とC#を使って、3Dアクションを作っています。
[やりたいこと]
スマホの時はtouches[0]のdeltaPositionを利用して、transform.positionとrotationを以下のコードのように使っております。
イメージとしては、白猫PJのような移動操作感を実現したいのですが、上手くいかず悩んでおります。(PCではできております。)
どなたか以下のスマホのコードでもPCの時のような操作ができるようにさせるためのお知恵を拝借できればと存じます。
C#
1void Update() 2 { 3// PCかスマホのどちらかで移動操作が行われたら 4if ((Mathf.Abs(Input.GetAxisRaw("Vertical")) > 0f || Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0f || Input.touchCount > 0)) 5 { 6 MoveField(); 7 } 8} 9 10private void MoveField() 11 { 12 // スマホ 13 if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Moved) 14 { 15 float spHorizontalVal = 0.0f; 16 float spVerticalVal = 0.0f; 17 18 Touch touch = Input.touches[0]; 19 // (Input.GetAxisRawは↑ or →なら+1で↓ or ←なら-1なので、それに合わせる) 20 if (touch.deltaPosition.x > 0f) 21 { 22 spHorizontalVal = -1; 23 } 24 else if (touch.deltaPosition.x < 0f) 25 { 26 spHorizontalVal = 1; 27 } 28 if (touch.deltaPosition.y > 0f) 29 { 30 spVerticalVal = -1; 31 } 32 else if (touch.deltaPosition.y < 0f) 33 { 34 spVerticalVal = 1; 35 } 36 // 回転 37 transform.Rotate(0, spHorizontalVal * rotateSpeed, 0); 38 // 移動 39 transform.position += transform.forward * spVerticalVal * speed; 40 } 41 // UnityEditor(PC) 42 else if (Mathf.Abs(Input.GetAxisRaw("Vertical")) > 0f 43 || Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0f) 44 { 45 // 回転 46 transform.Rotate(0, Input.GetAxisRaw("Horizontal") * rotateSpeed, 0); 47 // 移動(Input.GetAxisRawは↑ or →なら+で↓ or ←なら-) 48 transform.position += transform.forward * Input.GetAxisRaw("Vertical") * speed; 49 } 50 }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。