GimLee
September 4, 2015, 4:49pm
1
I have one boxcollider walking on the ground in 2d game.
From one frame to another, I want to expand the collider’s size.y from 0.8 to 3.0.
Now how much should I move the collider upwards, for it to be standing on the ground again?
My logic tells me this is the way, but I might be wrong:
collider.size = new Vector2(1.5f, 3f);
difference = 3f - 0.8f; // 2.2f
player.transform.position = new Vector2(player.transform.position.x, player.transform.position.y + difference * 0.5f);
I’m not changing the center or anything. But this results in the player hanging a little higher over the ground than I expected.
So can someone tell me if the math here is wrong? Or is it something else, causing the trouble?
Hello,
I’m not familiar with the 2d side of unity but if you resize the collider won’t it push your model off the ground… therefore moving the transform.position.y of it?
I would recommend to check your position.y before and after expanding and then see how much you need to adjust it.
but as said - just a shot in the dark
regards,
Should be a simple proportion.
Evaluate the actual distance from the ground before the scaling, then solve this:
heightBefore : 0.2 = x : newScale
Where x is the new height.
it will lead you to:
x = (heightBefore*newScale)/0.2
Doing so you can also keep it stuck on the ground by evaluating the distance every frame as you increase the scale to the final level.