Vector3.ClampMagnitude
static function ClampMagnitude(vector: Vector3, maxLength: float): Vector3;
static Vector3 ClampMagnitude(Vector3 vector, float maxLength);
static def ClampMagnitude(vector as Vector3, maxLength as float) as Vector3
Description

Returns a copy of vector with its magnitude clamped to maxLength.

	// Move the object around with the arrow keys but confine it
	// to a given radius around a center point.
	var centerPt: Vector3;
	var radius: float;
	
	
	function Update() {
		// Get the new position for the object.
		var movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		var 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.
		var offset = newPos - centerPt;
		transform.position = centerPt + Vector3.ClampMagnitude(offset, radius);
	}
using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {
    public Vector3 centerPt;
    public float radius;
    void Update() {
        Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        Vector3 newPos = transform.position + movement;
        Vector3 offset = newPos - centerPt;
        transform.position = centerPt + Vector3.ClampMagnitude(offset, radius);
    }
}
import UnityEngine
import System.Collections

public class Example(MonoBehaviour):

	public centerPt as Vector3

	public radius as float

	def Update() as void:
		movement as Vector3 = Vector3(Input.GetAxis('Horizontal'), 0, Input.GetAxis('Vertical'))
		newPos as Vector3 = (transform.position + movement)
		offset as Vector3 = (newPos - centerPt)
		transform.position = (centerPt + Vector3.ClampMagnitude(offset, radius))