カーソルでキャラクターを移動させ、壁ブロックにぶつかったらOnTriggerEnter2DとOnTriggerStay2Dで接触フラグを立てて、ぶつかったら座標を戻して移動制限をしているのですが、すり抜けてしまいます。(正確にはカクカクしてゆっくりすり抜けていきます。[下の画像参照])
そもそも障害判定がこの方針でいいのか疑問に思ってきたので質問してみました。
コライダーの設定と判定コードは以下のとおりです。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class arrow : MonoBehaviour { 6 7 private float PLAYER_SPEED = 50; 8 private Vector3 mLatestPosition; 9 private bool mCollided = false; 10 11 void Start () { 12 13 } 14 15 void Update () { 16 // カーソルによって移動 17 traslateByInput(); 18 } 19 20 void OnTriggerEnter2D(Collider2D col) { 21 Log.d("Enter", this.gameObject.transform.position); 22 mCollided = true; 23 } 24 void OnTriggerStay2D(Collider2D col) { 25 Log.d("Stay", this.gameObject.transform.position); 26 mCollided = true; 27 } 28 void OnTriggerExit2D(Collider2D col) { 29 Log.d("Exit", this.gameObject.transform.position); 30 mCollided = false; 31 } 32 33 void traslateByInput() { 34 float directionHorizontal = Input.GetAxisRaw ("Horizontal"); 35 float directionVertical = Input.GetAxisRaw ("Vertical"); 36 37 if (directionHorizontal != 0 || directionVertical != 0) { 38 Log.d("mCollided", mCollided); 39 if (mCollided) { 40 mCollided = false; 41 this.gameObject.transform.position = mLatestPosition; 42 } else { 43 float dx = directionHorizontal * PLAYER_SPEED * Time.deltaTime; 44 float dy = directionVertical * PLAYER_SPEED * Time.deltaTime; 45 this.gameObject.transform.Translate (dx, dy, 0); 46 mLatestPosition = this.gameObject.transform.position; 47 } 48 } 49 } 50} 51
▼環境
Unity2017 3.1f1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/27 07:02