How to make integer change by percentage based on gameobject movement?

Hey guys, I just wanted to run something by you, what would be the best way to go about changing an integer by percentage based on a gameobject’s position? What I mean by this is, if I move a gameobject by 10 perhaps. I want it to add 10% onto an integer of 0 if that makes sense or alternatively I’d be quite happy to get the same results without percentages it just depends on what works.

What I’ve got planned is I want to be able to move a gameobject and then have a script which makes other gameobjects react based on the position of the gameobject I’ve moved. Should I be reading directly from position data? Or should I create some sort of custom float that changes the integer for me?

You say other GOs should react based on the position of the GO you moved. Could you explain what “react” means?
I imagine how there are some objects around the player and if player moves right for 2 units other objects move right 2 units and keep same position relative to the player?
But I still don’t get why do you need this percentage. Can you explain your usecase? How other objects will use this knowledge about controlled object position %

Yes sorry I should be more detailed with my question, best way to show it is like this.


I’m pretty much trying to get something done where when you raise and lower the totem it’s by a certain percentage, that then causes a certain amount of people to go and do something, the villagers in this case would be the integer. I have a population integer already that counts how many villagers there are.

Hope that makes sense.

Ok, so you have the lowest position and the highest in this case. Get a distance between them and then current position of the totem relative to the highest position;

float totalDistance = Vector3.Distance(minPos, maxPos);
float currentDistance = Vector3Distance(totemPos, minPos);
var percentPassed = currentDistance / totalDistance;

Instead of Vector3 distance comparison you may just simply calc Y position. MinY will be on the statue bottom, MaxY will be the head. In this case:

var currentPositionRelativeToStatue = TotemY - MinY;
var statueHeight = MaxY - MinY;
var percentage = currentPositionRelativeToStatue / statueHeight;

Hope it helped

Or just use InverseLerp for the height:

1 Like

Thanks! Yes, that’s exactly how I have it, already got a min/max position float so I’ll give your code a try and see what happens.