﻿ Unity - スクリプティング API: Vector3.ClampMagnitude

• C#
• JS
• Boo

## スクリプト言語

お好みのスクリプト言語を選択すると、サンプルコードがその言語で表示されます。

# Vector3.ClampMagnitude

Suggest a change

## Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

## Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

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

## Description

```	// Move the object around with the arrow keys but confine it
// to a given radius around a center point.
var centerPt: Vector3;

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 ExampleClass : MonoBehaviour {
public Vector3 centerPt;
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 ExampleClass(MonoBehaviour):

public centerPt as Vector3