RigidBody2D Freeze Position in Y axis relative to parent

Hi all,

I’ve got a platform that is oscillating back and forth in the X axis, which has a BoxCollider2D and a RigidBody2D on it, and I have used freeze position in Z to prevent it from moving when hit by a ball in the scene. I am using RigidBody2D.MovePosition in a script to oscillate it.

I would now like to be able to rotate the platforms parent and have the platform oscillate in the X axis relative to the parent. However using freeze position in Z, and giving MovePosition only an X coordinate (which I believe is global, not local) the platform oscillates in the world X axis at an angle.

Is there a way to achieve the oscillation in the rotated axis?

Thanks in advance,
Andy

Instead of giving MovePosition just an X coordinate, try giving it the parent’s x axis direction

ParentObject.transform.right * distance

1 Like

Ah okay great, that has put me on the right track. The working code is:

mRb.MovePosition(transform.position + (transform.parent.right * (speed * mDirection * Time.deltaTime)));

Which is called in FixedUpdate, and mDirection is 1 or -1 based on which way it’s moving

1 Like