- Home /
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.
If anyone knows how to fix this problem it would be much appreciated. I'm completely stumped at this point.
So I'm still having trouble with this, and I thought I'd add some more information.
Like here is the script I'm using for movement. The player can only jump straight up using Unity rigidbody restrictions.
public float rotationSpeed = 500.0f;
public float jumpHeight = 14500.0f;
public float gravityForce = 500.0f;
public float startHeight = 0.8f;
public bool grounded = true;
//Jump Landing
void OnCollisionEnter ()
{
grounded = true;
}
void Update ()
{
//Gravity
this.GetComponent<Rigidbody> ().AddForce (Vector3.down * gravityForce);
//Jump
if (grounded)
{
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space))
{
this.GetComponent<Rigidbody> ().AddForce (Vector3.up * jumpHeight);
grounded = false;
}
}
if (!grounded)
{
//Forward Rotation
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow))
{
this.transform.Rotate (new Vector3 (0, 0, rotationSpeed * Time.deltaTime));
}
//Backwards Rotation
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow))
{
this.transform.Rotate (new Vector3 (0, 0, -rotationSpeed * Time.deltaTime));
}
}
}
The other thing I thought worth mentioning is that the player's transform goes crazy, only when the rotation is offset. If it is (0,0,0) it seems to work fine.
I feel like this is a simple fix I'm just missing something. I'm slowly going insane so a fix would be much appreciated.
Answer by tormentoarmagedoom · Sep 28, 2017 at 09:03 AM
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! :D
Thanks for trying to help me out. Though I tried calling the gameObject, and assigning the player as a gameObject, and calling that, but neither seemed to change it. When it lands on the platform while rotated, it still freaks out.
Whenever you can give my code a better look over would be a big help. :)
Answer by TheSandwichMan · Sep 29, 2017 at 02:07 AM
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.
Your answer
Follow this Question
Related Questions
Platform pathfinding 0 Answers
Spawning animals at a random postion on platforms. 2 Answers
2D 360 degress platformer example needed 0 Answers
Getting an object to re-spawn/ transform player back to the beginning 1 Answer
Player moving with platform 0 Answers