- Home /
Moving Platforms & Scale (Parent/Child)
Hi guys, I'm hoping you can help because this is driving me up the wall.
I'm still new to coding and Unity work in general, so if you could explain in detail what you mean, that would be great. I have gone through every single google page and Unity Answers page looking for answers.
My problem is this. I am creating a 2D platform game for a school assessment. In it, the player (currently a sphere, but will be replaced with an imported asset) moves onto a moving platform (simple collider) which will move either up and down, or left and right a set amount of space. The player needs to retain the ability to jump at will whilst on the platform. The way most people seem to get these to work is by having the player parented to the moving platform OnTriggerEnter or OnTriggerStay (this is currently in my player script) and OnTriggerExit, the parent/child relationship is broken.
This is all in C# btw.
void OnTriggerEnter(Collider otherObject)
{
if(otherObject.gameObject.tag == "MovingPlatform")
{
transform.parent = otherObject.transform;
}
}
void OnTriggerExit(Collider otherObject)
{
if(otherObject.gameObject.tag == "MovingPlatform"){
transform.parent = null;
}
This works well, and as it's a group assessment, I don't want to change the way these work in any significant way because it has been done by a partner. The problem is that when the player enters the MovingPlatform Collider, it inherits the parents scale properties. Because the platforms are scaled differently, this makes the player squish hideously. Searching far and wide, I found ways to change the scale of objects and included that into the OnEnter and OnExit functions, but it doesn't seem to work for OnEnter.
I have done it this way:
transform.localScale = new Vector3(1F, 1F, 1F);
As well as this way:
Vector3 scale = transform.localScale;
scale.y = 1F;
scale.x = 1F;
scale.z = 1F;
transform.localScale = scale;
I have read that the scale has to be relative to the platform, which is scaled 140,100,100. I have tried changing the scale values to accommodative for this, but it doesn't seem to work. I have read you may have to apply the scale to the player object (as you can see it's 1,1,1) inversely, but I am not sure how to do this.
Any help would really be beneficial. Please be aware that I have searched through the forums and answers for this, but nothing has helped so far. I have also tried to create an empty game object with scale 1,1,1 and use that as the parent of the platform, but the player still inherits the scale of the platform gameobject itself.
my advice is:
have your animator re-export the platform to the proper scale! scaling up models 140 times is bad practice, scale matters! scale it up in the animation software and re export, problem solved (posted as a comment b/c this wasn't really the question, but this is the proper way to handle this ;) )
edit:
it occurs to me you could probably get a similar solution by adjusting the FBX import settings inside unity as well (just select the fbx from the project panel and change the import scale). Adjusting the scale there should fix it, but my first suggestion still stands!
Good point Seth! In the future I will do that. As the assets were not created by me, I simply imported what I was given. In the future, it'd pay to throw them into $$anonymous$$aya and scale them according to the project before the madness begins.
And I didn't think to alter the import settings themselves. I can imagine that would also work. Thanks. I have fixed it now thanks to Ludeme Games, but I imagine this would have also done the job.
Seth Bergman, can you explain (or link) why scale matters? I am also here to learn. $$anonymous$$y first counterpoint would be that maybe having multiple instances of the imported object with different scale would be desirable in some cases, where having multiple fbx files would be undesirable.
Answer by Ludeme Games · Aug 06, 2012 at 04:00 AM
You could layer your hierarchy a little more to separate the position and scale of the platform.
For example: PlaneTransform(GameObject with Transform)->PlaneScale(GameObject with Transform and MeshFilter)
PlaneScale is a child of PlaneTransform.
Here you would modify PlaneScale to get the scaling of the mesh that you need, and PlaneTransforms position to move it around. When you parent the Player to PlaneTransform it will inherit only the position, and not the scale.
Ugh! I tried this previously but did it a little differently and it didn't work. Thank you! This time it worked marvellously. And thanks for being so fast! I can stop ripping my hair out and continue work!
I ended up needing to have three layers.
GameObject w/script > GameObject w/$$anonymous$$esh Filter > Actual Object (FBX)
@Ludeme Games Can you explain more precisely how to layer hiearchy??
It is pretty simple -
+ Plane Translate (Transform to modify translation)
|---- Player (All player components)
|---- Plane Scale ($$anonymous$$eshFilter, $$anonymous$$eshRenderer, and Transform to modify scale)
Both the Player and Plane Scale in the diagram are parented under Plane Translate.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Make gameobject a child without affecting scale? 1 Answer
Problem with child and parent. 1 Answer
Preserving children when irregularly scaling parent 1 Answer
make a child always have its own scale? 7 Answers