using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SWS;
public class nav_move : MonoBehaviour {
public bool play_on_load = false;
public PathManager path;
private Vector3[] road_data = null;
private int next_step = 0;
private float speed = 50000.0f; // 速度过快也没用
private float walk_time = 0.0f;
private float passed_time = 0.0f;
private bool is_walking = false;
// Use this for initialization
void Start () {
Vector3[] ctrl_points = path.GetPathPoints();
this.road_data = WaypointManager.GetCurved(ctrl_points);
if (this.play_on_load) {
this.walk_on_road();
}
}
void walk_on_road()
{
if (this.road_data.Length < 2)
{
return;
}
this.transform.position = this.road_data[0];
this.next_step = 1;
this.walk_to_next();
}
void walk_to_next()
{
if (this.next_step >= this.road_data.Length)
{
this.is_walking = false;
return;
}
Vector3 src = this.transform.position;
Vector3 dst = this.road_data[this.next_step];
Vector3 dir = dst - src;
float len = dir.magnitude;
if (len <= 0)
{
this.next_step++;
this.walk_to_next();
return;
}
this.is_walking = true;
this.walk_time = len / this.speed;
this.passed_time = 0;
this.transform.LookAt(dst);
}
// Update is called once per frame
void Update () {
if (!this.is_walking)
{
return;
}
float dt = Time.deltaTime;
this.passed_time += dt;
float real_walk_time = 0;
if (this.passed_time > this.walk_time)
{
real_walk_time = dt - (this.passed_time - this.walk_time); // 避免速度过快时跳跃
}
else
{
real_walk_time = dt;
}
float s = this.speed * real_walk_time;
this.transform.Translate(this.transform.forward * s , Space.World);
if (this.passed_time >= this.walk_time)
{
this.next_step++;
this.walk_to_next();
}
}
}
real_walk_time 的存在,如果speed过快,会被拉回来,所以没关系