0
votes

Say I have a point in a Vector3, and my FPSController (I'm using the standard one that comes with Unity 5) moves a magnitude of 10 away from this Vector3. I want to not allow movement, in any direction, beyond magnitude 10. Ideally, I would anticipate which direction the player pressed to move in, test that vector, and if it's below magnitude of 10 then it'll allow the movement to proceed. That way, if you're at 10 and press "back", you wont be able to move but if you press "forward" then no problem.

I know I'm being a bit abstract here. From what I understand the FPSController.cs script is using the CharacterController component. I've studied the FPSController code for awhile tonight and notice it's doing all sorts of calculations on the local position, but the magnitude needs to be between two world coordinates.

I know how to calculate the magnitude already, all I need to know is how to test the anticipated direction. I have a feeling it's easier than I think?

1
I don't get what you mean by "I want to not allow movement, in any direction, beyond magnitude 10." Are you saying that you want to constrain the player to within 10 units from a certain point? Or do you want to restrain their velocity?31eee384
I want to restrain them 10 units from a certain point. You're attached to a rope and once it's stretched to its length you can't go any furtherwizard_draziw

1 Answers

0
votes

You are overthinking this! Instead of thinking how you can constrain velocity, think about constraining position. Check out Vector3 Vector3.ClampMagnitude(Vector3, float), which returns the vector scaled to a maximum length. By "transforming" the player position to the target, clamping to the max length, then transforming back to world coordinates you can constrain the player's position.

// target: the Vector3 you can't get too far from.
// distance: the float max distance from the target.
transform.position = Vector3.ClampMagnitude(transform.position - target, distance) + target;