Making a Child/Parent Based Moving Platform?

So I’m learning C# and am messing around with a shift platform. I’ve seen many people make a simple script to add the player to the platform as a child, making it move with the platform. However, as I’ve played around with this I’ve found that when my player (a simple cube) get parented, it’s position and more so it’s scale go crazy.

This is my script for the platform.

public float platformSpeed = 2.5f;
public bool direction = true; //true = left, false = Right
public GameObject player = null;
private Vector3 scale;

void FixedUpdate ()
{
	//Left Direction
	if (this.transform.position.x >= 6 || this.transform.position.x > -6 && direction) 
	{
		this.transform.Translate (Vector3.left * (platformSpeed * Time.deltaTime));

		if (this.transform.position.x <= -6) 
		{
			direction = false;
		}
	}

	//Right Direction
	else if (this.transform.position.x <= -6 || this.transform.position.x < 6 && !direction) 
	{
		this.transform.Translate (Vector3.right * (platformSpeed * Time.deltaTime));

		if (this.transform.position.x >= 6) 
		{
			direction = true;
		}
	}
}

And this is a just for the player to stick to platforms, movement is another script.

void OnCollisionStay (Collision platform)
{
	this.transform.parent = platform.transform;
}

void OnCollisionExit ()
{
	this.transform.parent = null;
}

Also here’s just an image of what it looks like.

102694-glitchy.png

If anyone knows how to fix this problem it would be much appreciated. I’m completely stumped at this point.

Good day!!

One tip:

When you say “this” in the script, you are refearing to this script atached to the gameobject, not the gameobject. For some functions, it works as gameobject, but if you are used to do it, you will have errors for sure.

If want to refear the object where the script is placed, use “gameObject” (not GameObject which is a class)

void OnCollisionStay (Collision platform)
 {
     gameObject.transform.parent = platform.transform;
 }
 void OnCollisionExit ()
 {
     gameObject.transform.parent = null;
 }

Then ,talking about childs and parents. You must care of relations of parameters like rotations, scales, positions… when child-unchild an object. If the parent and the child does not ahve the same scale/rotation, it can give you problems.

As a good practice, if you want to parent/unparent objects during runtime, i recomend you to create an empty GameObject that have all the real objects to be linked as childs, in the same “level of parenthood”. This way, the parent for all will have always scale (1,1,1), rotation (0,0,0), etc… so will not be affected if parent/unparent from it.

Please Accept the answer as correct and/or ask more using @tormentoarmagedoom !

Bye! :smiley:

I just want to show how I fixed this issue for anyone else who had this issue. So like @tormentoarmagedoom said, you want a third party object to parent the player and the platform too. Though, if you just make an empty game object at (0,0,0) it won’t make the play stick to the platform, though that might just be the way I’m moving my platform.

So instead I parented the platform, to an empty game object, and then called the empty game object up in the script to then parent to the player.

void OnCollisionStay (Collision platformCollision)
{				
	GameObject platformCenter = platformCollision.transform.GetChild(0).gameObject;

	gameObject.transform.parent = platformCenter.transform;	
}
	
void OnCollisionExit ()
{
	gameObject.transform.parent = null;
}

I had also added an if statement to check for a tag “Platform”, because if an object doesn’t have a child you will get an error. So that was just a little fix for that.

Anyways I hope this helps anyone in the future who has this same issue, it’s maddening I know.