For my game I need to make two objects share the same x and y transform value, I want both of them to move but only one to be movable by the player. I have added a rigid body to the one the player can move. Is it possible to make it so the position of another object would change according to the one with the rigid body?
If two objects have the same transform values, then they will be in the exact same positions, overlapping one another. Do you mean, you want two objects that move together but only one is controllable?
I guess either case, this can be done in two ways:
-
Parent one object (dummy object) to the other (controllable object). Moving the parent will also move the children GameObjects, while respecting their local position. This can be done in the Editor’s hierarchy or in code:
public Transform dummyTransform;
void Start()
{
dummyTransform.parent = transform;
}
Transform.parent
-
Update the dummy object’s position from a script attached to the controllable object.
public Transform dummyTransform;
Vector3 lastPosition;void Start()
{
lastPosition = transform.position;
}void Update()
{
Vector3 difference = transform.position - lastPosition;
dummyTransform.Translate( difference );
lastPosition = transform.position;
}