版本: Unity 6 (6000.0)
语言英语
  • C#

Vector3.ClampMagnitude

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们会阅读用户提出的每项建议更改,并在适用的情况下进行更新。

关闭

提交失败

由于某种原因,您的建议更改无法提交。请<a>稍后重试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public static Vector3 ClampMagnitude(Vector3 vector, float maxLength);

描述

返回一个 vector 的副本,其大小限制为 maxLength

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); } }