1.创建一个名为“ FlythoughController ”c#角本,用以下代码替换默认代码:
using UnityEngine;
using System.Collections;
public class FlythoughController: MonoBehaviour {
FlythoughWaypoint[] waypoints;
MyBezier bezier;
int currentWaypoint = 0;
public float SecondsForFullLoop = 200f;
//~ private float incremental = 0f;
private Vector3 endPosition;
static int ID = 0;
private int Was = 0;
private float currentTime = 0;
void Start () {
Component[] rawArray = transform.parent.GetComponentsInChildren(typeof(FlythoughWaypoint));
if (rawArray.Length == 0)
{
Debug.LogError("You have no 'FlythroughWaypoint' objects defined. Create an empty object at the same level as this and add the FlythoughWaypoint script to it");
}
waypoints = new FlythoughWaypoint[rawArray.Length];
rawArray.CopyTo(waypoints, 0);
bezier = new MyBezier(waypoints);
//~ for( int i = 0; i < rawArray.Length; i ++ ){
//~ if((transform.position-waypoints[ID].CurrentPosition).magnitude>(transform.position-waypoints[i].CurrentPosition).magnitude){
//~ ID = i;
//~ }
//~ }
//~ Vector3 newPosition = bezier.GetPointAtTime(0.01f);
}
void Update () {
float dt = Time.deltaTime;
if(Was==0){
for (float t = 0; t < 1f; t += 0.01f){
if((transform.position-bezier.GetPointAtTime(currentTime / SecondsForFullLoop)).magnitude>(transform.position- bezier.GetPointAtTime(t)).magnitude){
currentTime=t*SecondsForFullLoop;
}
}
Was = 1;
return;
}
//~ incremental += dt;
currentTime +=0.1f ;
double Temp = SecondsForFullLoop-0.5;
if( currentTime > Temp ){
currentTime = 0f;
}
Vector3 newPosition = bezier.GetPointAtTime(currentTime / SecondsForFullLoop);
print("currentTime=="+currentTime);
print("newPosition=="+newPosition);
Quaternion rotation = Quaternion.LookRotation( newPosition - transform.position );
transform.rotation = Quaternion.Lerp( transform.rotation,rotation,dt );
transform.position = Vector3.Lerp( transform.position,newPosition,dt );
}
//~ void Update () {
//~ float dt = Time.deltaTime;
//~ incremental += dt;
//~ float currentTime = incremental % SecondsForFullLoop;
//~ double Temp = SecondsForFullLoop-0.5;
//~ if( currentTime > Temp ){
//~ incremental = 0f;
//~ }
//~ Vector3 newPosition = bezier.GetPointAtTime(currentTime / SecondsForFullLoop);
//~ print("currentTime=="+currentTime);
//~ Quaternion rotation = Quaternion.LookRotation( newPosition - transform.position );
//~ transform.rotation = Quaternion.Lerp( transform.rotation,rotation,dt );
//~ transform.position = Vector3.Lerp( transform.position,newPosition,dt );
//~ }
private Vector3 NextWaypoint()
{
if (currentWaypoint + 1 > waypoints.Length - 1)
{
return waypoints[0].CurrentPosition;
}
else
{
return waypoints[currentWaypoint + 1].CurrentPosition;
}
}
void OnDrawGizmosSelected()
{
DrawGizmoStuff();
}
public void DrawGizmoStuff()
{
Component[] rawArrayGiz;
FlythoughWaypoint[] waypointsGiz;
rawArrayGiz = transform.parent.GetComponentsInChildren(typeof(FlythoughWaypoint));
waypointsGiz = new FlythoughWaypoint[rawArrayGiz.Length];
rawArrayGiz.CopyTo(waypointsGiz, 0);
MyBezier myBezierGiz = new MyBezier(waypointsGiz);
for (float t = 0; t < 1f; t += 0.001f)
{
Gizmos.DrawIcon(myBezierGiz.GetPointAtTime(t), "WaypointHeading.tif");
}
}
}
///
2.创建一个名为“ FlythoughWaypoint
”的c#角本,用以下代码替换默认代码:
using UnityEngine;
using System.Collections;
public class FlythoughWaypoint : MonoBehaviour {
public Vector3 CurrentPosition
{
get
{
return transform.position;
}
}
void OnDrawGizmos()
{
Gizmos.DrawIcon(transform.position, "Waypoint.tif");
}
void OnDrawGizmosSelected()
{
(transform.parent.GetComponentInChildren(typeof(FlythoughController)) as FlythoughController).DrawGizmoStuff();
}
}
//
3.创建一个名为“ MyBezier ” 的c#角本,用以下代码替换原代码:
using UnityEngine;
using System.Collections;
public class MyBezier {
Vector3[] vectorArray;
public MyBezier(FlythoughWaypoint[] waypointsArray)
{
vectorArray = new Vector3[waypointsArray.Length];
for (int i = 0; i < waypointsArray.Length; i++)
{
vectorArray[i] = waypointsArray[i].CurrentPosition;
}
}
public Vector3 GetPointAtTime(float t)
{
return CreateBenzierForPoint(t);
}
private Vector3 CreateBenzierForPoint(float t)
{
int x = (int) (t * (float)vectorArray.Length);
if (x == vectorArray.Length)
{
x = 0;
}
// This is based off http://homepage.mac.com/nephilim/sw3ddev/bezier.html
float newT = (t * (float)vectorArray.Length) - (float)x;
Vector3 prevl = vectorArray[CheckWithinArray(x, vectorArray.Length)];
Vector3 thisl = vectorArray[CheckWithinArray(x + 1, vectorArray.Length)];
Vector3 nextl = vectorArray[CheckWithinArray(x + 2, vectorArray.Length)];
Vector3 farl = vectorArray[CheckWithinArray(x + 3, vectorArray.Length)];
Vector3 delta1 = (nextl - prevl) * .166f;
Vector3 delta2 = (farl - thisl) * .166f;
return DoBenzierForNPoints(newT, new Vector3[] { thisl, thisl + delta1, nextl - delta2, nextl });
}
private int CheckWithinArray(int x, int c)
{
if (x >= c)
{
return x % c;
}
else
{
return x;
}
}
private Vector3 DoBenzierForNPoints(float t, Vector3[] currentArray)
{
Vector3 returnVector = Vector3.zero;
float oneMinusT = (1f - t);
int n = currentArray.Length - 1;
for (int i = 0; i <= n; i++)
{
returnVector += BinomialCoefficient(n, i) * Mathf.Pow(oneMinusT, n - i) * Mathf.Pow(t, i) * currentArray[i];
}
return returnVector;
}
#region Standard Maths methods
private float BinomialCoefficient(int n, int k)
{
if ((k < 0) || (k > n)) return 0;
k = (k > n / 2) ? n - k : k;
return (float) FallingPower(n, k) / Factorial(k);
}
private int Factorial(int n)
{
if (n == 0) return 1;
int t = n;
while (n-- > 2)
t *= n;
return t;
}
private int FallingPower(int n, int p)
{
int t = 1;
for (int i = 0; i < p; i++) t *= n--;
return t;
}
#endregion
}
//
4.
先建立一个 empty 1 然后 在 它下面多建立几个 empty 虚拟物体 给他们WAYPOINT 脚本 之后建立一个 方块 也托到 empty 1下 给他 CONTROLLER 脚本 按开始 就走动了