2017-04-21 6 views
0

私は、特定のパスに従うことになっていたスクリプトを作成しました。ここに私のスクリプトです。しかし、スクリプトを実行すると何も起こりません。実際には、カプセルは経路の子オブジェクトに向かって移動するはずだった。私はVector3.movetowardを使用して試してみましたが、何も、あなたがMoveTowardsたびに新しい位置を計算するが、割り当てることはありませんしているあなたのユニティパスの次のスクリプトは機能しませんか?

transform.position = Vector3.MoveTowards (this.transform.position, targetdestination, 5f * Time.deltaTime); 

を変換するためにMoveTowardsによって返された値を割り当てる必要が

using System.Collections.Generic; 
using UnityEngine; 

public class Guard : MonoBehaviour { 
    public Transform pathholder; 
    private Vector3 startposition; 
    private Vector3 previousposition; 
    private float waittime=1.5f; 
    private Vector3[] waypoints; 
    Vector3 playerinitialposition; 
    void Start() { 
     Vector3[] waypoints = new Vector3[pathholder.childCount]; 
     for (int x = 0; x < pathholder.childCount; x++) { 
      waypoints [x] = pathholder.GetChild (x).position; 
     } 
     startposition = pathholder.GetChild (0).position; 
     previousposition = startposition; 
     playerinitialposition = new Vector3 (startposition.x, this.transform.position.y, startposition.z); 
     this.transform.position = playerinitialposition; 
     StartCoroutine(Followpath(waypoints)); 
    } 


    void Update() { 

    } 

    void OnDrawGizmos(){ 
     foreach (Transform waypoint in pathholder) { 
      Gizmos.DrawSphere (waypoint.transform.position,1f); 
      Gizmos.DrawLine (previousposition, waypoint.position); 
      previousposition = waypoint.position; 
     } 
    } 

    IEnumerator Followpath (Vector3[] waypoints){ 
     int lengthinindex = 1; 
     Vector3 targetdestination = waypoints[lengthinindex]; 
     while (true) { 
      Vector3.MoveTowards (this.transform.position, targetdestination, 5f * Time.deltaTime); 
      if (this.transform.position == targetdestination) { 
       lengthinindex = (lengthinindex + 1) % waypoints.Length; 
       targetdestination = waypoints [lengthinindex]; 
       yield return new WaitForSeconds (waittime); 
      } 
     } 
     yield return null; 
    } 
} 

答えて

1

を起こりませんあなたのオブジェクトの実際の位置に移動します。 MoveTowards Documentation.

+0

ありがとう私は笑っているように感じています –

+0

@SamratLuitelあなたは大歓迎です:) – CNuts

関連する問題