# ClampMagnitude

> Creates a copy of a given Vector3 with its magnitude clamped to a maximum length.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.5/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

## ClampMagnitude(Vector3, float)

Creates a copy of a given Vector3 with its magnitude clamped to a maximum length.

```csharp
public static Vector3 ClampMagnitude(Vector3 vector, float maxLength)
```

### Parameters

**** (\[Vector3]\(/engine/6000.5/script-reference/unityengine/vector3)): The vector to copy.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The maximum length of the returned vector.

### Returns

| Type                                                              | Description                                                   |
| ----------------------------------------------------------------- | ------------------------------------------------------------- |
| [Vector3](/engine/6000.5/script-reference/unityengine/vector3.md) | A copy of `vector` with its magnitude clamped to `maxLength`. |

### Examples

```csharp
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    // Move the object around with the arrow keys but confine it
    // to a given radius around a center point.

    public Vector3 centerPt;
    public float radius;

    void Update()
    {
        // Get the new position for the object.
        Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        Vector3 newPos = transform.position + movement;

        // Calculate the distance of the new position from the center point. Keep the direction
        // the same but clamp the length to the specified radius.
        Vector3 offset = newPos - centerPt;
        transform.position = centerPt + Vector3.ClampMagnitude(offset, radius);
    }
}
```

## ClampMagnitude(Vector3, float)

Creates a copy of a given Vector3 with its magnitude clamped to a maximum length.

```csharp
public static Vector3 ClampMagnitude(in Vector3 vector, float maxLength)
```

### Parameters

**** (\[Vector3]\(/engine/6000.5/script-reference/unityengine/vector3)): The vector to copy.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The maximum length of the returned vector.

### Returns

| Type                                                              | Description                                                   |
| ----------------------------------------------------------------- | ------------------------------------------------------------- |
| [Vector3](/engine/6000.5/script-reference/unityengine/vector3.md) | A copy of `vector` with its magnitude clamped to `maxLength`. |

### Examples

```csharp
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    // Move the object around with the arrow keys but confine it
    // to a given radius around a center point.

    public Vector3 centerPt;
    public float radius;

    void Update()
    {
        // Get the new position for the object.
        Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        Vector3 newPos = transform.position + movement;

        // Calculate the distance of the new position from the center point. Keep the direction
        // the same but clamp the length to the specified radius.
        Vector3 offset = newPos - centerPt;
        transform.position = centerPt + Vector3.ClampMagnitude(offset, radius);
    }
}
```
