Hi,
I have two objects, A and B. I want to interactively move A and have B move in the opposite direction or
scale the movement of b so that it travels half the distance of a.
.
I can lock their positions using b.transform.position = a.transform.position;
but I can’t seem to get the result I’m after.
Any ideas?
Thanks,
Jeff
public void TranslateObjects( Transform ObjectA, Transform ObjectB, Vector3 translation )
{
ObjectA.Translate( translation );
ObjectB.Translate( -translation ); // Move opposite direction
// ObjectB.Translate( translation * 0.5f ); // Move half the distance ObjectA has travelled
// ObjectB.position = ObjectA.position ; // Copy position of A
}
Then, you will have to use this function to translate ObjectA. Otherwise, you could check each frame the distance A travelled, but it’s obvisouly less optimized.
public Transform ObjectA;
public Transform ObjectB;
private Vector3 lastObjectAPosition ;
void Update()
{
TranslateObjectB( ObjectB, ObjectA.position - lastObjectAPosition ) ;
lastObjectAPosition = ObjectA.position ;
}
public void TranslateObjectB( Transform ObjectB, Vector3 translation )
{
ObjectB.Translate( -translation ); // Move opposite direction
// ObjectB.Translate( translation * 0.5f ); // Move half the distance ObjectA has travelled
// ObjectB.position = ObjectA.position ; // Copy position of A
}
this is great. Thanks!
I got it working by simply writing:
void Update () {
b.transform.position = a.transform.position * -1;
}
I’m new to coding so I’m going to give yours a try and figure out why yours is better.
Jeff