|
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();
}
}
//////////////////////////////////////////////////////////////////
4.
先建立一个 empty 1 然后 在 它下面多建立几个 empty 虚拟物体 给他们WAYPOINT 脚本 之后建立一个 方块 也托到 empty 1下 给他 CONTROLLER 脚本 按开始 就走动了
5.创建一个名为“Gizmos”的“Folder”,把下面两个图片放进“Gizmos”文件中 |
评分
-
查看全部评分
|