Unity3d路径巡游(改良)

原文地址:Unity3d路径巡游(改良) 作者:血孩子

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 脚本 按开始 就走动了

5.创建一个名为“Gizmos”的“Folder”,把下面两个图片放进“Gizmos”文件中

[转载]Unity3d路径巡游(改良)

注:由于不能上传tif格式,故些为截屏图片,大家可以放到ps里面另保存为tif格式
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值