SetParent worldPositionStays not working

So I’m trying to create a moving platform that keeps the player by making the platform the player’s parent. Like many others who choose this method, I ran into the problem of the player’s scale and rotation changing radically upon moving on the platform. After more investigation, I saw that there was a “worldPositionStays” parameter in the SetParent function.

I have tried to use this, but the result was the player falling through the platform (which does not happen under any other circumstance). Would anyone have an idea of what the problem is? Here is the relevant section of code:

private void OnCollisionEnter(Collision other)
	if (other.transform.tag == "Moving Platform") {
		transform.SetParent (other.transform, false);

If anyone needs more information, I’m happy to oblige. If it turns out that this second parameter does not actually fix the problem, I would appreciate that as well.

The “worldPositionStays” Parameter

The reason why the player is falling through the platform is because setting the “worldPositionStays” parameter to false makes sure that the position value of the player doesn’t change. For example, if the platform is at (3, 4) and the player is at (4, 5), when the player is set as a child of the platform, it’s new local position would normally become (1, 1). With that parameter false, it’s local position stays at (4, 5), which makes it’s global position (7, 9).

Potential Solution

If it’s the rotation and scale of the platform you’re worried about, what I would do is have an empty GameObject that has the actual platform visual as a child. So the graphics have the modified rotation and transform and this empty GameObject would only store the position (Rotation would be 0, 0, 0 and scale 1, 1, 1). Then you can just set the player’s parent to this empty GameObject and work for there. Any scripts you have on the actual platform should move to the empty GO too. The player’s scale and rotation should be fine and work as usual this way, as it has the same values as the “global parent”

I’m not sure if it will achieve what you’re going for, but from what it sounds like I think it would work.

I hope this helped!