- Home /
Moving an object along a prespecified trajectory according to coordinates from arrays
Dear community,
I've been looking for some hours in all along relevant web resources for the following problem but with no progress ... I want to move objects along prespecified trajectories in the space. The trajectories I specify via absolute x and z coordinates (2D-trajectory), which are arranged in separated arrays. Here is my code so far. The coordinates are just examples, nevertheless, I want to have the object moving from the previous in the array coordinate point to the next one:
var x = new Array (1000, 1500, 1500, 1500, 1000, 500, 500, 500);
var z = new Array (500, 500, 1000, 1500, 1500, 1500, 1000, 500);
var cnt = x.length;
var i=0;
function Start () {
for (i=0; i<=cnt; i++) {
transform.position.x = x[i];
transform.position.z = z[i];
}
}
So, for each point to on the trajectory the machine shall iterate over all the array elements. This should give as first coordination point (according to the array above) x1z1(1000, 500), the second x2y2(1500, 500) etc. When I run the script, however, the object doesn't do anything. I've also tried it via translate.position = Vector3(x[i], 0, z[i]) within the loop, and also here without the desired result.
Does anybody have an idea how to approach this problem? I thank you in advance for any help.
Answer by devast3d · Oct 11, 2011 at 08:43 PM
Antares project is already have what you are looking for: example of moving object alongside a spline. http://forum.unity3d.com/threads/47696-Antares-Project-(Open-Source)-1.5.5-Coming-soon
Answer by DaveA · Oct 11, 2011 at 07:52 PM
It would do all that iteration in the first frame rendered. You need to spread it out using an Update function. You may want to google 'waypoint following' also. And you can use arrays of Vector2 or Vector3 if you want to, to keep things tidy.
So specify some speed, and in the Update function you can use Math.Slerp to move the object along the current segment given Time.deltaTime. Look in the scripting manual for examples.
I suppose another way would be to insert a 'yield WaitForSeconds(1);' in the loop, so it would jump from point to point every second. But depends on what you want.
Your answer
Follow this Question
Related Questions
Gameboard, next position on a path 2 Answers
Is there any option to put objects together? 0 Answers
move from one point to another 2 Answers
Move object to several locations 2 Answers