前提・実現したいこと
プレイヤーがクリックしたところまで移動したら待機状態になるというものを実装
したいです
発生している問題・エラーメッセージ
処理は合っているが、今の構文だと待機するアニメーションのif文まで行かないと感じました。 どこを変えればいいでしょうか?
該当のソースコード
c#
1 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine.AI; 5using UnityEngine.UI; 6using UnityEngine; 7 8public class Move: MonoBehaviour { 9 public Transform targetPosition; //移動する位置 10 public GameObject DestroyButton; 11 public GameObject GatheringButton; 12 public GameObject StopProcessingButton; 13 public string Terraintag = "Terrain"; 14 private NavMeshAgent agent; 15 private RaycastHit hit; 16 private Ray ray; 17 private Animator animator; 18 19 20 // 到着フラグ 21 private bool arrived; 22 static Canvas _canvas; 23 private const string Trigger_isWait = "is_Wait"; 24 25 // Use this for initialization 26 void Start () 27 { 28 // Canvasコンポーネントを保持 29 _canvas = GetComponent<Canvas>(); 30 DestroyButton.SetActive(false); 31 GatheringButton.SetActive(false); 32 agent = GetComponent<NavMeshAgent>(); 33 animator = GetComponent<Animator>(); 34 arrived = false; 35 } 36 37 // Update is called once per frame 38 void Update () 39 { 40 if(Input.GetMouseButton(0)) 41 { 42 Debug.Log("move"); 43 ray = Camera.main.ScreenPointToRay(Input.mousePosition); 44 if(!arrived) 45 { 46 if(Physics.Raycast(ray ,out hit ,100)) 47 { 48 Debug.Log(hit.point);//デバッグログにクリックした場所を出す 49 if(hit.collider.gameObject) 50 { 51 agent.SetDestination(hit.point); 52 53 const float NearDistance = 0.4f; 54 if(Vector3.Distance(targetPosition.position ,hit.point) < NearDistance) 55 { 56 targetPosition.position = hit.point; 57 arrived = true; 58 Debug.Log("arrived"); 59 this.animator.SetTrigger(Trigger_isWait); 60 } 61 } 62 } 63 } 64 } 65 } 66} 67
試したこと
クリックしたところとプレイヤーが同じ位置になると待機アニメーションになる
のような処理を書いたが、Rayで飛ばしたところにプレイヤーが移動する処理は
実際移動した時に若干のズレがあることが判明しました。どのような処理をすれば
待機アニメーションに遷移するでしょうか?
補足情報(FW/ツールのバージョンなど)
Unity 2017.3.1f1
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/29 05:33
2018/06/29 07:00 編集
2018/06/29 08:05
2018/07/01 12:11 編集
2018/07/02 05:14