- Home /
Multiple objects follow the object in front
private float amplitudeY = 3.0f;
private float omegaZ = 5.0f;
private float omegaY = 1.0f;
private float timeI;
public EnemySpawn eSpawn;
public int place;
//keeps current position of objects
public List<Vector3> posList = new List<Vector3>();
public void Snaker()
{
Vector3 leader = eSpawn.eSnake [0].transform.position;
posList.Add (new Vector3 (eSpawn.eSnake[0].transform.position.x,
eSpawn.eSnake[0].transform.position.y,
eSpawn.eSnake[0].transform.position.z));
Vector3 v = posList [0];
Vector3 toLeader = leader - transform.position;
Quaternion wantDir = Quaternion.LookRotation(toLeader);
timeI += Time.deltaTime;
float z = omegaZ * timeI;
float y = Mathf.Abs (amplitudeY * Mathf.Sin (omegaY * timeI));
eSpawn.eSnake [place].transform.position = new Vector3 (0.0f, v.y - y, v.z - z);
transform.rotation = Quaternion.RotateTowards(transform.rotation, wantDir, 360*Time.deltaTime);
transform.position = leader - (toLeader.normalized * 1.5f);
}
Currently thats what I have.
Unfortunately what happens is object 1 follows object 0 just fine, but objects 2-5 all stack onto object 1.
How can I add a space between each of the objects?
Answer by Noah Dyer · Jan 14, 2015 at 07:38 PM
I believe you problem is your toLeader. For all your other variables, you appear to be determining relationships through your through an array, but your toLeader variable is set to the specific object that this script is attached to. my best guess is that the line needs to become
Vector3 toLeader = leader - eSpawn.eSnake [place].transform.position;
Added that, the object just sits there.
If I add
place++;
if(place >=5)place = 0;
the objects just snap all over the place.
When is Snaker() called? Do all 5 objects (the leader and followers) have this script? You're setting place in the editor then?
public void Snaker()
Yes
No. I tried both with and without Place++ to check if either would work.
I'm thinking about using Dicitionaries? Would that work?
@Nasareth$$anonymous$$ekuri's logic helped me figure out what I think your problem actually is. You're always following the in index 0. Ins$$anonymous$$d, you should follow "place - 1", no?
Wouldn't that just do the same, except for the last object in the list?
Or give me an Argument out of range error.
Answer by khos85 · Jan 14, 2015 at 10:38 PM
Why not just use built in unity hinges on each object with a spring on each?
Oh it's used with Rigidbodies. Those don't agree to much when it comes to being easy to work with.
why not ? :) what issue have you got with hinges, can you provide more details? I reckon that would work nicely if you got the hinges configured well..
Yeah Im trying to get them to work, but I don't really understand what I'm suppose to do with them.
Like how am I suppose to get the objects to move forward, all they do is rotate.
Or why won't the Connected Body connect to the rigidbody?
If possible, can I parent the "Head" object to a node, and attack a rigidbody to the node, then connect the "Body" objects to the node?
Answer by MKayJay · Jan 14, 2015 at 10:58 PM
I believe this is some kind of snake game? Wouldn't it be easier for you to keep track of how "long" your snake is, and then use that number to make the object follow the one just in front of it? So object 1 follows object 0- object 2 follows object 1, and so on?
If this is not a snake game, you could still use the "numbers" to give each object some kind of offset so that they won't stay on top of each other.
Yeah, I was thinking of doing an Id system for each part.
I think the biggest issue is that Im trying to control multiple bodies with one script. It doesn't seem to like that very much.
Answer by RaulG · Jan 15, 2015 at 12:58 AM
public List<Vector3> posList = new List<Vector3>();
public void Snaker()
{
posList.Add (eSpawn.eSnake [0].transform.position);
posList.Add (eSpawn.eSnake [1].transform.position);
posList.Add (eSpawn.eSnake [2].transform.position);
posList.Add (eSpawn.eSnake [3].transform.position);
posList.Add (eSpawn.eSnake [4].transform.position);
Vector3 v0 = posList [0];
Vector3 v1 = posList [1];
Vector3 v2 = posList [2];
Vector3 v3 = posList [3];
Vector3 v4 = posList [4];
float amplitudeY = 3.0f;
float omegaY = 1.0f;
timeI += Time.deltaTime;
float y = Mathf.Abs(amplitudeY * Mathf.Sin(omegaY * timeI));
eSpawn.eSnake [0].transform.position = new Vector3 (0.0f, v0.y + y, v0.z -1.0f * timeI);
eSpawn.eSnake [1].transform.position = new Vector3 (0.0f, v1.y + y, v1.z -1.0f * timeI);
eSpawn.eSnake [2].transform.position = new Vector3 (0.0f, v2.y + y, v2.z -1.0f * timeI);
eSpawn.eSnake [3].transform.position = new Vector3 (0.0f, v3.y + y, v3.z -1.0f * timeI);
eSpawn.eSnake [4].transform.position = new Vector3 (0.0f, v4.y + y, v4.z -1.0f * timeI);
}
It's not the cleanest thing ever. But I basically did what @NasarethMekuri said to do. Only problem is they all move the exact same.
Your answer
Follow this Question
Related Questions
What am I doing wrong with my AI? 1 Answer
Controlling AI Movement 0 Answers
Player moves too fast and won't move to point 1 Answer
Code Not Working 1 Answer
How do I get GameObjects to look at me? 2 Answers