Formula to anchor and line up box collider?

34 Views Asked by At

I think this is largely a math problem and it is not exclusive to unity, here is my issue:

I am trying to line up the offset of my box collider 2D whenever i change its Y size.

I need a formula that anchors the bottom of the collider and changing the Y size just makes the collider grow upwards.

To do this atm I found the base offset I like in an arbitrary way (0.4842f) for an arbitrary size.Y (1)

I am then doing:

BoxCol.offset.y =  0.4842f * BoxCol.size.y;

The problem is that this formula is apparently bad and does not do what I want. Here is a sketch of what is happening for clarity:

enter image description here

I obviously have the wrong formula, what is the formula that when the size.y = 0.1f will give me the offset.y ~= 0.034f? and at the same time, with the size.y = 1 will give me the offset.y = 0.4842?

I hope I explained the problem properly, basically I want to be able to change the size of the collider but the bottom edge should be anchored to the same point always, the collider should only grow upwards.

I dont want to change the pivot of my sprite since I am using it for other things, I am looking for a pure math solution that takes the Y size and produces the correct Y offset so the bottom bound of the collider never changes position.

Thanks for the help

BTW I dont know if this matters, but the scale of the object the collider component is sitting on is (4,4,1), so the object is scaled up by 4x essentially

0

There are 0 best solutions below