Hi all, I want to move my objects with waypoints array, some thing like this:
in start I’m placing objects to start positions:
for(int i = 0; i < points.Length-2; i++)
{
objects[i].position = points[i].position;
}
But cannot figure out how to move them
Ok, here is what I have for now:
using UnityEngine;
using System.Collections;
public class MoveTest : MonoBehaviour {
public Transform[] obj;
public Transform[] points;
public int index;
void Start ()
{
for (int i = 0; i < points.Length - 8; i++)
{
obj.position = points.position;
}
}
void Update()
{
if (Input.GetMouseButton(0))
Move();
}
void Move()
{
for (int i = 0; i < obj.Length; i++)
{
float distance = Mathf.Abs((obj.position - points[index].position).magnitude);
if (distance > 0.001F)
{
if (index != 0)
obj.position = Vector3.MoveTowards(obj.position, points[index].position, 0.3F * Time.deltaTime);
else
obj.position = points[index].position;
}
else
index++;
if (index == points.Length)
index = 0;
}
}
}
This works but only for 1 object…
Have each object move itself. You’ve already pretty much figured out how to get an object to follow a set of waypoints. So, write a script that moves just “this” object along a set of waypoints. Then put that script on each object you want to move (possibly starting at a different point in the waypoints array, if needed).
This is way easier, and for many reasons better, than trying to have one master controller script that moves a bunch of objects.
JoeStrout:
Have each object move itself. You’ve already pretty much figured out how to get an object to follow a set of waypoints. So, write a script that moves just “this” object along a set of waypoints. Then put that script on each object you want to move (possibly starting at a different point in the waypoints array, if needed).
This is way easier, and for many reasons better, than trying to have one master controller script that moves a bunch of objects.
I thought about it, but this way dosen’t suit my needs… Anyway I’ve already done it with a class with positions and index for each object.