Description
The AddClamped
method adds a specified vector to the current vector and ensures that the resulting vector's length does not exceed a specified maximum length. This method is useful for operations where you want to add a vector but need to ensure that the resulting vector does not exceed a certain magnitude, which can be important in physics calculations or when dealing with movement constraints.
Usage
To use the AddClamped
method, call it on an instance of Vector3
, passing in the vector to add and the maximum length as parameters. The method will return a new Vector3
that is the result of the addition, clamped to the specified maximum length.
Example
Vector3 currentPosition = new Vector3(1.0f, 2.0f, 3.0f);
Vector3 vectorToAdd = new Vector3(0.5f, 0.5f, 0.5f);
float maxLength = 3.0f;
Vector3 result = currentPosition.AddClamped(ref vectorToAdd, maxLength);
// result is a Vector3 that is the sum of currentPosition and vectorToAdd, but clamped to a length of 3.0f.