2012年2月23日 星期四

【程式範例】 導航點 - waitpoint

修改後的導航點,在導航點間移動速度相同
複製此程式到需要導航的物件上
再設定路徑座標陣列(新增cube當導航點拖曳到waypoints裡)

此程式碼修改自:http://answers.unity3d.com/questions/12859/move-an-object-through-a-set-of-positions.html
// array to hold waypoint locations
var waypoints: Transform[];
// variable to control time taken to travel between points
private var startPoint: Vector3;
private var endPoint: Vector3;
private var startTime: float;

// the array index number of the current target waypoint
private var targetwaypoint: int;

function Start() {
    startPoint = transform.position;
    startTime = Time.time;

    if (waypoints.Length <= 0) {
        Debug.Log("No waypoints found");
        enabled = false;
    }

    targetwaypoint = 0;
    endPoint = waypoints[targetwaypoint].position;
}

function Update() {
    var duration: float = (Vector3.Distance(startPoint, endPoint) / 5);
    var i = (Time.time - startTime) / duration;

    transform.position = Vector3.Lerp(startPoint, endPoint, i);

    if (transform.position == endPoint) {
        startTime = Time.time;

        // increment and wrap the target waypoint index
        targetwaypoint++;
        targetwaypoint = targetwaypoint % waypoints.Length;
        startPoint = endPoint;
        endPoint = waypoints[targetwaypoint].position;
    }
}

1 ♥:

  1. 請問如何撰寫GUI自動導航,就是按BUTTON就會自動開始找waypoint

    回覆刪除