Сожалеем, но текст оригинала доступен только зарегистрированным пользователям. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class Waypoints_Controller : MonoBehavior { [ExecuteInEditMode] // this script simply gives us a visual path to make it easier to edit // our waypoints private ArrayList transforms; // arraylist for easy access to // transforms private Vector3 firstPoint; // store our first waypoint so we can // loop the path private float distance; // used to calculate distance between // points private Transform TEMPtrans; // a temporary holder for a transform private int TEMPindex; // a temporary holder for an index number private int totalTransforms; private Vector3 diff; private float curDistance; private Transform closest; private Vector3 currentPos; private Vector3 lastPos; private Transform pointT; public bool closed=true; public bool shouldReverse; void Start() { // make sure that when this script starts that // we have grabbed the transforms for each waypoint GetTransforms(); } void OnDrawGizmos() { // we only want to draw the waypoints when we're editing, // not when we are playing the game if( Application.isPlaying ) return; GetTransforms(); // make sure that we have more than one transform in the // list, otherwise we can't draw lines between them if (totalTransforms < 2) return; // draw our path first, we grab the position of the very // first waypoint so that our line has a start point TEMPtrans = (Transform)transforms[0]; lastPos = TEMPtrans.position; // we point each waypoint at the next, so that we can use // this rotation data to find out when the player is going // the wrong way or to position the player after a reset // facing the correct direction. So first we need to hold a // reference to the transform we are going to point pointT = (Transform)transforms[0]; // also, as this is the first point we store it to use for // closing the path later firstPoint = lastPos; // now we loop through all of the waypoints drawing lines // between them for (int i = 1; i < transforms.Count; i++) { TEMPtrans = (Transform)transforms[i]; if(TEMPtrans==null) { GetTransforms(); return; } // grab the current waypoint position currentPos = TEMPtrans.position; Gizmos.color=Color.green; Gizmos.DrawSphere(currentPos,2); // draw the line between the last waypoint and this one Gizmos.color=Color.red; Gizmos.DrawLine(lastPos, currentPos); // point our last transform at the latest position pointT.LookAt(currentPos); // update our 'last' waypoint to become this one as we // move on to find the next... lastPos = currentPos; // update the pointing transform pointT=(Transform)transforms[i]; } // close the path if(closed) { Gizmos.color=Color.red; Gizmos.DrawLine(currentPos, firstPoint); } } public void SetReverseMode(bool rev) { shouldReverse=rev; } public void GetTransforms() { // we store all of the waypoints transforms in an ArrayList, // which is initialised here (we always need to do this // before we can use ArrayLists) transforms=new ArrayList(); // now we go through any transforms 'under' this transform, // so all of the child objects that act as our waypoints get // put into our arraylist foreach(Transform t in transform) { // add this transform to our arraylist transforms.Add(t); } totalTransforms=(int)transforms.Count; } public int FindNearestWaypoint ( Vector3 fromPos, float maxRange) { // make sure that we have populated the transforms // list, if not, populate it if(transforms==null) GetTransforms(); // the distance variable is just used to hold the // 'current' distance when we are comparing, so that // we can find the closest distance = Mathf.Infinity; // Iterate through them and find the closest one for(int i = 0; i < transforms.Count; i++) { // grab a reference to a transform TEMPtrans = (Transform)transforms[i]; // calculate the distance between the current // transform and the passed in transform’s // position vector diff = (TEMPtrans.position - fromPos); curDistance = diff.sqrMagnitude; // now compare distances - making sure that // we are not closer than the closest object // (whose distance is held by the variable // (distance) if ( curDistance < distance ) { if( Mathf.Abs( TEMPtrans.position.y - fromPos.y ) < maxRange ) { // set our current 'winner' // (closest transform) to the // transform we just found closest = TEMPtrans; // store the index of this // waypoint TEMPindex=i; // set our 'winning' distance // to the distance we just // found distance = curDistance; } } } // now we make sure that we did actually find // something, then return it if(closest) { // return the waypoint we found in this test return TEMPindex; } else { // no waypoint was found, so return -1 (this // should be accounted for at the other end!) return -1; } } // this function has the addition of a check to avoid finding the // same transform as one passed in. We use this to make sure that // when we are looking for the nearest waypoint we don’t find the // same one as we just passed public int FindNearestWaypoint ( Vector3 fromPos , Transform exceptThis, float maxRange) { // make sure that we have populated the transforms // list, if not, populate it if(transforms==null) GetTransforms(); // the distance variable is just used to hold the // 'current' distance when we are comparing, so that we can find the closest distance = Mathf.Infinity; // Iterate through them and find the closest one for(int i = 0; i < transforms.Count; i++) { // grab a reference to a transform TEMPtrans = (Transform)transforms[i]; // calculate the distance between the current // transform and the passed in transform’s // position vector diff = (TEMPtrans.position - fromPos); curDistance = diff.sqrMagnitude; // now compare distances - making sure that // we are not if ( curDistance < distance && TEMPtrans != exceptThis ) { if( Mathf.Abs( TEMPtrans.position.y - fromPos.y ) < maxRange ) { // set our current 'winner' // (closest transform) to the // transform we just found closest = TEMPtrans; // store the index of this // waypoint TEMPindex=i; // set our 'winning' distance // to the distance we just // found distance = curDistance; } } } // now we make sure that we did actually find // something, then return it if(closest) { // return the waypoint we found in this test return TEMPindex; } else { // no waypoint was found, so return -1 (this // should be accounted for at the other end!) return -1; } } public Transform GetWaypoint(int index) { if( shouldReverse ) { // send back the reverse index'd waypoint index=(transforms.Count-1)-index; if(index<0) index=0; } // make sure that we have populated the transforms list, if // not, populate it if(transforms==null) GetTransforms(); // first, let's check to see if this index is higher than // our waypoint count // if so, we return null which needs to be handled on the // other side' if(index>transforms.Count-1) return null; return (Transform)transforms[index]; } public int GetTotal() { return totalTransforms; } }
ilia1395
25.10.14 в 21:49
|
|
|
|
|
void Start() { // make sure that when this script starts that // we have grabbed the transforms for each waypoint GetTransforms(); }
ilia1395
25.10.14 в 21:49
|
|
|
|
|
public void GetTransforms() { // we store all of the waypoints transforms in an ArrayList, // which is initialised here (we always need to do this // before we can use ArrayLists) transforms=new ArrayList();
ilia1395
25.10.14 в 21:52
|
|
|
|
|
foreach(Transform t in transform) { // add this transform to our arraylist transforms.Add(t); } totalTransforms=(int)transforms.Count; }
ilia1395
25.10.14 в 21:52
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OnDrawGizmos() { // we only want to draw the waypoints when we’re editing, // not when we are playing the game if( Application.isPlaying ) return;
ilia1395
25.10.14 в 21:52
|
|
|
|
|
|
|
|
GetTransforms(); // make sure that we have more than one transform in the // list, otherwise we can't draw lines between them if (totalTransforms < 2) return; // draw our path first, we grab the position of the very // first waypoint so that our line has a start point TEMPtrans = (Transform)transforms[0]; lastPos = TEMPtrans.position; // we point each waypoint at the next, so that we can use // this rotation data to find out when the player is going // the wrong way or to position the player after a reset // facing the correct direction. So first we need to hold a // reference to the transform we are going to point pointT = (Transform)transforms[0]; // also, as this is the first point we store it to use for // closing the path later firstPoint = lastPos; // now we loop through all of the waypoints drawing lines // between them for (int i = 1; i < totalTransforms; i++) { TEMPtrans = (Transform)transforms[i]; if(TEMPtrans==null) { GetTransforms(); return; } // grab the current waypoint position currentPos = TEMPtrans.position;
ilia1395
25.10.14 в 21:52
|
|
|
|
|
Gizmos.color=Color.green;
ilia1395
25.10.14 в 21:52
|
|
|
|
|
Gizmos.DrawSphere(currentPos,2); // draw the line between the last waypoint and this // one Gizmos.color=Color.red; Gizmos.DrawLine(lastPos, currentPos);
ilia1395
25.10.14 в 21:52
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pointT.LookAt(currentPos);
ilia1395
25.10.14 в 22:01
|
|
|
|
|
|
|
|
|
|
|
// update our 'last' waypoint to become this one as // we move on to find the next... lastPos = currentPos; // update the pointing transform pointT=(Transform)transforms[i]; } // close the path if(closed) { Gizmos.color=Color.red; Gizmos.DrawLine(currentPos, firstPoint); } }
ilia1395
25.10.14 в 22:02
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void SetReverseMode(bool rev) { shouldReverse=rev; } public int FindNearestWaypoint (Vector3 fromPos, float maxRange) { // make sure that we have populated the transforms // list, if not, populate it if(transforms==null) GetTransforms(); // the distance variable is just used to hold the // 'current' distance when we are comparing, so that // we can find the closest distance = Mathf.Infinity;
ilia1395
25.10.14 в 22:02
|
|