找回密码
 立即注册
查看: 4577|回复: 86

[脚本] Unity3d路径巡游 轉

[复制链接]
发表于 2014-3-1 23:00 | 显示全部楼层 |阅读模式

 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”文件中

评分

参与人数 4鲜花 +4 收起 理由
715614441 + 1 很给力!
1287537617aa + 1
aa1287537617 + 1
npshj + 1

查看全部评分

发表于 2014-7-2 22:22 | 显示全部楼层
你的这个 MyBezier bezier 类 没有给出啊
发表于 2017-2-20 07:44 | 显示全部楼层
顶顶多好
发表于 2017-2-20 08:14 | 显示全部楼层
真心顶
发表于 2017-2-20 08:01 | 显示全部楼层
说的非常好
发表于 2017-2-20 08:08 | 显示全部楼层
很好哦
发表于 2017-2-20 07:32 | 显示全部楼层
不错不错
发表于 2017-3-28 16:36 | 显示全部楼层
很不错
发表于 2017-3-28 16:35 | 显示全部楼层
楼主是超人
发表于 2017-3-28 16:31 | 显示全部楼层
好帖就是要顶
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-12-23 21:30 , Processed in 0.118782 second(s), 31 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表