Hello all,
I need to increase my c# knowledge, I can’t really understand how static and return values are meant to be declared…
The following script uses a simple waypoints system, really cool as it doesn’t require to assign waypoints manually, nore to set the array count, but just picks up all the transforms from a given game object.
:°D
Now I’m attempting to get the closest waypoint from the array list.
Array is working properly. tmpDist and Dist are correct too…
But the “FindClosest” function is not working yet.
;°(
I guess I need a little explanation about how to deal with array items
:°p
- Should the FindClosest function be static or void?
- what is to be declared within brackets in the function FindClosest ()?
Thanks in advance for your help…
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Waypoints : MonoBehaviour {
public Transform myTransform;
public GameObject waypointContainer;
List <Transform> waypoints ;
Transform[] potentialWaypoints;
public float Dist;
public float tmpDist;
public int bestWaypoint;
public int currentWaypoint;
public float currentAngle;
Vector3 RelativeWaypointPosition;
Vector3 RelativeWaypointDir;
//////////////////////////// waypoints ///////////////////////
void Start ()
{
GetWaypoints();
FindClosest(); ///// tried this (bestWaypoint); //(potentialWaypoints[]);
currentWaypoint = bestWaypoint;
}
void GetWaypoints ()
{
if (waypointContainer!=null)
{
Transform[] potentialWaypoints = waypointContainer.GetComponentsInChildren <Transform> ();
waypoints = new List <Transform> ();
foreach (Transform potentialWaypoint in potentialWaypoints)
{
for (int i=0;i<waypoints.Count;i++);
if (potentialWaypoint != waypointContainer.transform)
waypoints.Add (potentialWaypoint);
// Debug.Log("foundpotentialWaypoint");
}
}
}
void FindClosest () { /////tried this: (Transform[] waypoints) {
Dist = (waypoints[0].transform.position - transform.position).sqrMagnitude;
waypoints[bestWaypoint] = waypoints[0];
for (int i=1;i<waypoints.Count;i++) {
tmpDist = (waypoints*.transform.position - transform.position).sqrMagnitude;*
-
if (tmpDist < Dist)*
_ waypoints[bestWaypoint] = waypoints*;_
_ }_
_// return waypoints[bestWaypoint];_
_ }*_