Hi all, wanted to create a waypoint that allow the object to randomly walk. Any recommended reading or approach to focus on? thanks.
var waypoint : Transform[ ];
var speed : float = 20;
private var currentWaypoint : int;
var loop : boolean = true;
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++;
}
else{
velocity = moveDirection.normalized * speed;
}
}
else{
if(loop){
currentWaypoint=0;
}
else{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
transform.LookAt(target);
}
when i apply the codes above, i keep getting the error message of:
IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) System.Object:stelemref (object,intptr,object)
waypoint.Awake () (at Assets\waypoint.js:7)[/b]
anyone know wads wrong with the code? or do i need to change any setting in related to unity3d? like resoultion, size, etc?
You are using what they called Unity built in array. Those have a define size, which you can change by writing waypoint = new Transform [10], or public var waypoint : Transform [ ] = new Transform [10]; for example (it will have 10 slots, with index going from 0 to 9.
Currently, your waypoint Transform list has a size of 0 so you can’t assign anything.