#実現したいこと
Rayを飛ばして移動方向にオブジェクトが存在しないことを確認してからその方向に移動を行う。
#発生している問題
Rayの長さは6fに設定しているのに5fの距離の壁を判定せず
オブジェクトが壁をすり抜けて移動する。
#試したこと
有効かどうかはさておき、RaycastではなくSphereCastを試してみたが解決せず。
コードは以下の通りです。
C#
1Vector3 targetpos; 2 Vector3 nowpos; 3 float time; 4 5 void Start() 6 { 7 nowpos = transform.position; 8 targetpos = nowpos; 9 } 10 11 void Update() 12 { 13 time += Time.deltaTime; 14 transform.position = Vector3.Lerp(nowpos, targetpos, time); 15 16 if (time > 1.0f) 17 { 18 DirSerch(); 19 time = 0f; 20 } 21 } 22 23 void DirSerch() 24 { 25 nowpos = transform.position; 26 27 Physics.Raycast(nowpos, Vector3.up, out RaycastHit hit_up, 6f); 28 Physics.Raycast(nowpos, Vector3.right, out RaycastHit hit_right, 6f); 29 Physics.Raycast(nowpos, Vector3.down, out RaycastHit hit_down, 6f); 30 Physics.Raycast(nowpos, Vector3.left, out RaycastHit hit_left, 6f); 31 32 float value = Random.value; 33 34 if (value < 0.25f && hit_up.collider == null) 35 { 36 targetpos = nowpos + new Vector3(0, 0, 10f); 37 } 38 else if (value < 0.5f && hit_right.collider == null) 39 { 40 targetpos = nowpos + new Vector3(10f, 0, 0); 41 } 42 else if (value < 0.75f && hit_down.collider == null) 43 { 44 targetpos = nowpos + new Vector3(0, 0, -10f); 45 } 46 else if (value < 1.0f && hit_left.collider == null) 47 { 48 targetpos = nowpos + new Vector3(-10f, 0, 0); 49 } 50 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。