- Home /
Move multiple objects relative to another objects movement
I am trying to put together a little script that that moves multiple objects relative to another objects movements. These objects are not grouped to the "parent" object, and are not using the parenting feature of Unity. Below is an example of how I am currently attempting to accomplish this, but the Y axis keeps flipping from negative to positive on each update, which causes the objects to appear as if they are flashing.
foreach (GameObject piece in PlayerPieces)
{
Vector3 offset = (this.transform.position - piece.transform.position);
if (isAlive)
{
piece.transform.position = this.transform.position + offset;
}
}
Answer by tobyfee · Mar 16, 2014 at 12:51 AM
This doesn't look like the correct vector math. The problem, as I see it, is that 'offset' goes the entire distance from the piece to the target (I'm going to call 'this' the target for clarity's sake). Setting the piece's position to be the same as the target plus the offset means it will move to the target, then through the target the same distance and direction it needed to move to the target.
That can't be right, but I'm not totally certain of your goal: did you want the pieces to move toward the target constantly, like a heat-seeking missile, or do you want it to just move along with the target, like the way an umbrella moves with the person carrying it?
To get it the objects to maintain a fixed relationship to the target, parenting is the easiest way to go about it, but you mentioned you don't want to do that.
The other way to accomplish the same effect would be to capture the relationship between the two objects right at start and preserve it. e.g.
here be pseudocode
Public GameObject target; Private Vector3 offset;
void Start(){
offset = target.transform.position - this.transform.position;
}
void Update(){
this.transform.position = target.transform.position + offset;
}
Note that in this version the script is on the piece and not the central object, which will probably make more sense in the long run.
For what I am trying to do your analogy of the umbrella is perfect, I am trying to get a few objects to follow along with another object. In my case it is multiple objects, and I know that just applying a parent to the objects would be the easiest way, but it makes the rest of my code way to complicated.
What I have in my "controller" class is a generic object list, of the children of the controller, and each time there is movement on the controller the child positions are updated in relation to the controller's position.
public List<GameObject> PlayerPieces = new List<GameObject>();
void UpdatePlayerPieces()
{
foreach (GameObject piece in PlayerPieces)
{
Vector3 offset = (this.transform.position - piece.transform.position);
if (isAlive)
{
piece.transform.position = piece.transform.position + offset;
}
}
}
I guess I could take the above code and have each game object have a link to the controller object, rather than the controller object linking to all of the child objects. That seems like it would be the best way, but it would require more rigging on my part which is fine. I will give that a shot, thank you for your help.
Your answer
Follow this Question
Related Questions
Random Movement : 2d 1 Answer
Vector3 Returning Infinity Error 2 Answers
A node in a childnode? 1 Answer
Change int value for 30 sec... 1 Answer
object movement as wave in Unity 3d 2 Answers