I have tried setting my player object as a child to a moving platform so that it would follow it when moving, but my player doesn’t move with the platform. Super strange…
In my game I am trying to move the platform with the mouse, and I’ve tried 3 different methods: setting the raw position, changing the rigidbody velocity, and adding force.
Method 1 - Setting the raw position
rbody.position = Vector2.Lerp(rbody.position, mouse, Time.deltaTime * speed);
Method 2 - Changing the rigidbody velocity
rbody.velocity = Vector2.ClampMagnitude(deltaDist * speed, maxSpeed * 10);
Method 3 - Adding force
rbody.AddForceAtPosition(deltaDist * speed, transform.position);
But none worked. In the hierarchy I have it setup like this
-
Platform
-
Player
-
Platform collider
The platform parent contains the script for moving the platform with the mouse, and a rigidbody 2D. The platform collider contains the sprite renderer and collider 2D for the parent. I have separated the platform and it’s collider to be able to scale the sprite and it’s collider without scaling the player when it’s set as a child to the platform.
I have been googling around for solutions to make the player follow a platform and making it a child of the platform on collision seems as the best option, but for some reason as said earlier it doesn’t follow the platform parent.
I am double checking and the platform parents transform is changing and not the collider child, but the player objects transform is changing in reverse, to counter the transform changes instead of following them.
I’ve tried disabling all extra scripts and everything but I can’t get it to work. Any ideas why this is happening?