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

Description

Returns a copy of vector with its magnitude clamped to 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); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961