How do I make two objects share two of the same transform values?
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?
Answer by Jessespike · Mar 08, 2016 at 07:21 PM
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:
1) 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;
}
2) 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;
}
Your answer
Follow this Question
Related Questions
how to make the player move in only 2 directions? 0 Answers
Move GameObject with transform.position 1 Answer
Character bounces off walls, and drops to the ground on movement 1 Answer
rigidybody transform rotation & position mutually exclusive? 0 Answers
Moving spawned enemies randomly towards several pre-defined positions 1 Answer