Radomizing Waypoints

Hi,

I am quite new to Unity and I would like to have my character choose a waypoint
based from a random number.

I will have four child waypoints to choose from at each parent waypoint.
I was thinking I could have an array which could would hold all the parent waypoints , which in turn hold 4 child waypoints each however im not sure how to implement this.

Im currently using a basic waypoint system below but need to add to it to get the randomness working.

var Waypoint : Transform[];
static var speed : float = 0.5;
private var currentWaypoint : int;

    function Update () 
    {
        if(currentWaypointArray < Waypointscontainer.length)
        {					
            var target : Vector3 = Waypoint[currentWaypoint].position;
            var moveDirection : Vector3 = target - transform.position;
            var velocity = rigidbody.velocity;
            
            if(moveDirection.magnitude < 0.25 )
            {
                currentWaypoint++;      	 
            }			
            else
            {
                velocity = moveDirection.normalized*speed;
            }       
        }
        else
        {
          currentWaypoint = 0;
        }  
        rigidbody.velocity = velocity;
    }

Any help would be greatly appreciated.

Thanks

Jacob

I am indeed using the latest version. I've added two images below to show what i did. [116360-conditionalfield.png|116360] [116361-conditionalfieldresukt.png|116361] Is there something that i am doing wrong? I've put your "ConditionalFieldAttribute.cs" in Assets folder and "ConditionalFieldAttributeDrawer.cs" file to Assets/Editor folder. It works with normal fields but it doesn't hide arrays. Thanks for the help!

2 Answers

2

currentWaypointIndex = Random.Range(0, arrayLength); // tada

Oh, I didn't test out my attribute properly, simply never put it on array field. I dug into this issue and found out that PropertyDrawers simply don't work with arrays. Attribute drawer will only work on every element in the array and not on array itself. Unfortunately :( In current state my attribute will throw exceptions if you'll try to add array elements, I'll update it to simply skip arrays

Hi
Here is solution for random waypoint selection
var loop:boolean=true;
var waypoint:Transform;
var speed:float=20;
private var currentWaypoint:int;

function Awake () {
waypoint[0]=transform;
}

function Update () {
if(currentWaypoint<waypoint.length){
var target:Vector3=waypoint[currentWaypoint].position;



var moveDirection:Vector3=target-transform.position;
var velocity=rigidbody.velocity;
if(moveDirection.magnitude<1){
currentWaypoint= Random.Range(0,waypoint.Length);
}
else{
velocity=moveDirection.normalized*speed;
}
}
else{
if(loop){
currentWaypoint=0;
}
else{
velocity=Vector3.zero;
}
}
rigidbody.velocity=velocity;
}

That is exactly what i tryed to explain, different properties/values/behaviour should go to different classes, try to look from OOP and unity's components point of view, in other words: use composition whenever you can