- Home /
problem moving character between multiple nodes, storing next target and previous target in array and moving between them (JS)
ive been trying to create a script that stores multiple nodes in a list as transforms and allows a player to move between them using the A and D keys, it works fine going through the array forwards with D but i cant seem to get it to move through the array whilst storing the current node and previous node so it moves between them. im pretty new to coding and a lot of this is adapted through various searches ive done trying to solve the problem. heres what i have -
import System.Collections.Generic;
import System.Linq;
var wayPoints : List.<Transform>;
var currentTarget : Transform;
var speed : float = 4000;
var turnSpeed = 40;
function Awake ()
{
// creates node array, adds nodes tagged "Node", sets up initial target node
wayPoints = new List.<Transform>();
AddNodes();
TargetNode();
}
function Update ()
{
// creates target vector to rotate towards on key press
var currentTargetDir = currentTarget.position - transform.position;
var step = turnSpeed * Time.deltaTime;
var currentDir = Vector3.RotateTowards(transform.forward, currentTargetDir, step, 0.0);;
if (Input.GetKey("d"))
{
transform.rotation = Quaternion.LookRotation(currentDir);
rigidbody.AddRelativeForce (Vector3.forward * speed * Time.deltaTime);
}
// here i want to have it so pressing A rotates the player to the previous node and moves towards it and then continues
// going backwards through the array until D is pressed and then it moves forwards though array again.
//
// if (Input.GetKey("a"))
// {
// transform.rotation = Quaternion.LookRotation(previousTargetDir);
// rigidbody.AddRelativeForce (Vector3.forward * speed * Time.deltaTime);
// }
// when player is 2 units from the target node, stop velocity and change target to next in the array
//
var distance : float = Vector3.Distance(currentTarget.transform.position, transform.position);
if(distance < 2f)
{
rigidbody.velocity.x = 0;
rigidbody.velocity.z = 0;
TargetNode();
}
}
function AddNodes()
{
var nodeArray : GameObject[] = GameObject.FindGameObjectsWithTag("Node");
for (var node : GameObject in nodeArray)
{
AddTarget(node.transform);
}
}
function AddTarget (node : Transform)
{
wayPoints.Add(node);
wayPoints = wayPoints.OrderBy(function(x){return x.name;}).ToList();
}
// have tried to store previousTarget by keeping it one step behind next but it only goes back to the last node and then
// stops
function TargetNode()
{
if(currentTarget == null)
{
currentTarget = wayPoints[0];
}
else
{
var next : int = wayPoints.IndexOf(currentTarget);
}
if(next < wayPoints.Count - 1)
{
next++;
}
else
{
next = 0;
}
currentTarget = wayPoints[next];
}
some things ive tried -
creating a variable alongside next in TargetNode() that stays one step behind it but it only returns to previous not back through array
using next++ or next-- based on diection its moving
created two seperate TargetNode() based on direction and/or proximity to previoustarget or current target, one ++ one --
duplicating the list and reversing it, linking previous target var to this.
sorry for a long post but ive been going over and over this and i cant get it to acheive the node based movement i want, any suggestions or advice would be appreciated, like i said im new to coding.. please no suggesting iTween or splinecontroller or any anything, ive been through them and it doesnt work how i want it to, thanks in advance.
Your answer
![](https://koobas.hobune.stream/wayback/20220613171848im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
FindClosestEnemy() change target? 1 Answer
How do I return the index of an array of sprites as an int? 1 Answer
How to see what element is the current one 2 Answers
Problem with List and Array? 2 Answers