- Home /
How can I reverse my node list?
So this script is for 5 different AI that basically go from a vending machine to their desk but i want them to come back again in the same order they went there. Each AI has about 5 nodes, i've tried a few things but yea its driving me nuts..
var forwardSpeed : float= 1;
public var waypoint : Transform[];
private var pointA : Vector3;
private var currentWaypoint : int;
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 * forwardSpeed;
}
rigidbody.velocity = velocity;
}
}
sorry took so long.. yea that confuses me a tad, not that great at program$$anonymous$$g but i assume its this line else{if(loop){currentwaypoint=0;}}} im looking for, because if i could not use ur entire script i get addition points for using my own stuff ins$$anonymous$$d on pinching yours
if you would be so kind i honestly thought it would be something as simple as (blahblah).Reverse lol .Guess not its just a 2D isometric game for a rapid protype and yea reversing the path is pretty much all i have left... as long as they travel between the desk and vending machine via a route of atleast 5 nodes im happy
Answer by AlucardJay · Nov 13, 2012 at 07:19 AM
I have tested this and it is working. I included a variable to change whether the waypoint count is going up or down (wayDir), then 2 checks if currentWaypoint > waypoint.length or < 0.
#pragma strict
var forwardSpeed : float = 1;
public var waypoint : Transform[];
private var pointA : Vector3;
private var currentWaypoint : int;
private var wayDir : int = 1;
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 += wayDir;
Debug.Log( "currentWaypoint " + currentWaypoint );
if ( currentWaypoint >= waypoint.length )
{
currentWaypoint = waypoint.length - 1;
wayDir = -1;
}
else if ( currentWaypoint < 0 )
{
currentWaypoint = 0;
wayDir = 1;
}
Debug.Log( " : currentWaypoint " + currentWaypoint + " : wayDir " + wayDir );
}
else
{
velocity = moveDirection.normalized * forwardSpeed;
}
rigidbody.velocity = velocity;
//}
}
well it wont let me thumbs it for some reason but it works thank you so much.. I havnt come across wayDir yet but something for the memory .
And thanks delta for trying to help appreciate the effort
I think you can only thumb (like) when you have enough karma. To accept the answer, just click on the tick under the thumbs, the answer then should have a green background (and your karma rises by accepting answers!).
wayDir is just a variable I made up. It is simply a value of +1 or -1.
So if wayDir == 1 then currentWaypoint += wayDir; means currentWaypoint += 1; (or ++)
Then if wayDir == -1 then currentWaypoint += wayDir; means currentWaypoint += -1; (or --)
It just changes the direction of the count when you use currentWaypoint += wayDir; either going up or down (thats why I left the Debugs in, to demonstrate currentWaypoint going up then down).
(wayDir is my abbreviation for Waypoint Direction !)
=]
Answer by deltamish · Nov 13, 2012 at 05:35 AM
Hi, What are you are trying to do is called waypoint looping I have posted a script for the above here It is an AI script
read the Update function of my script it contains looping to test it just copy the required code excluding animation code if you wish.it may seem a bit long but it is a very simple one.The animation code is making it long
if you are feeling confused i shall post another script for you only looping and moving part
Answer by deltamish · Nov 13, 2012 at 06:37 AM
okay heres a simple script
var waypoint:Transform[];
var currentwaypoint:int;///Sets the number to infinity
var loop:boolean = false;
var character: CharacterController;
var rotationspeed:float = 1.9;
function Update(){
if(currentwaypoint < waypoint.Length){
var way = waypoint[currentwaypoint].position;///start of detecting waypoint pos
var direction:Vector3 = way - transform.position;
if(direction.magnitude < 1){
currentwaypoint++;
character.Move(direction.normalized * walkspeed * Time.deltaTime);
//////////to rotate to wards the waypoint(optional)///////
transform.eulerAngles = Vector3(0,transform.eulerAngles.y,0);
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(direction),rotationspeed * Time.smoothDeltaTime);
/////////////////////////
}////end of detecting waypoint pos
} else {/////detecting wether to loop or not
if(loop){
currentwaypoint = 0;
}
}///end of detecting loop
}////end of update
I have given a lot of space between the codes to make them easier to understand Sorry for the late reply
unknown identifier walkspeed should i just add a variable var walkspeed:float = 1.9;
@deltamish : Edit your other answer with this information, don't post 2 answers :/
@ImPVEspec : I cannot read your code. For further help, please format your code in the question. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
Though, it may be easier just to delete the code, paste it in again from the source, then highlight all the code and press the 101010 button at the top of the edit window before posting.
sorry i didnt know that
@deltamish i dont have anything to put in the var character: CharacterController; sorry i dont mean to be nub
No problem, it is much easier to read now =]
I have submitted an answer ($$anonymous$$ $$anonymous$$ay)
Your answer
Follow this Question
Related Questions
A* Algorithm Node Grid Generation 2 Answers
A* endless loop 0 Answers
Making a list of list of nodes for Kruskal 2 Answers
How can I manually link/unlink a node in a path? 0 Answers
Node Pathfinding 1 Answer