
画面の右側をタップすると右に、左側をタップすると左に移動スクリプトを書きました。
しかし、開始とともにオブジェクトが右に永久に移動してしまいます。
ご教授願います。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class BardPosition : MonoBehaviour { 6 public float scroll = 1f; 7 float direction = 1f; 8 Rigidbody2D rb2d; 9 10 void Start () { 11 rb2d = GetComponent<Rigidbody2D>(); 12 } 13 14 void Update () { 15 if (Input.touchCount > 0) { 16 Touch touch = Input.GetTouch(0); 17 //画面右半分をタッチしていたら右に移動 18 if(touch.position.x > Screen.width * 0.5f) { 19 direction = 1f; 20 //画面左半分をタッチしていたら左に移動 21 } else if (touch.position.x < Screen.width * 0.5f) { 22 direction = -1f; 23 } else { 24 direction = 0f; //←何もタッチしていない場合は反応なし 25 } 26 } 27 rb2d.velocity = new Vector2(scroll * direction, rb2d.velocity.y); 28 } 29}

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/07/08 08:42
2018/07/08 12:46
退会済みユーザー
2018/07/10 10:07