Legacy Documentation: Version 4.6(go to latest)
Language: English
  • C#
  • JS
  • Boo

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Vector3.ClampMagnitude

public static function ClampMagnitude(vector: Vector3, maxLength: float): 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);
	}