- Home /
Constraining an object's movement in one direction by range
OK.... Here is What I have been trying to do... Mario style left to right movement.
I want my character to move left side onle 8.0 unit regardless of its current position. When I push left arrow or "A" button, I shouldn't be able to move it further than 8.0 unit...
What is your suggestion ? ( except for parenting it to another gameObject)
Yes, I have looked at this math operation. But I do not think that It would work in an open space ... let's say our character at the origin. And we move it at (120,0,0)... Whn I want to move it back, It should be at (112,0,0)... then we move it again and now it is at (234,0,0).... press the left arrow and we want the object at (226,0,0)... always 8 unit in negatif direction.... How can we constraint something very dynamic with a clamp operation?
You have to keep track of the maximum forward progress. Then in Javascript your code would be:
if (transform.position.x < maximumPosition - 8.0) {
transform.position.x = maximumPosition - 8.0;
}