Mathf.Clamp 限制

static function Clamp (value : float, min : float, max : float) : float

Description描述

Clamps a value between a minimum float and maximum float value.

限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void Update() {
		transform.position = new Vector3(Mathf.Clamp(Time.time, 1.0F, 3.0F), 0, 0);
	}
}
// Set the position of the transform to be that of the time
// but never less than 1 or more than 3
//随着时间设置变换位置
//但是不会小于1或大于3
function Update () {
	transform.position = Vector3(Mathf.Clamp(Time.time, 1.0, 3.0), 0, 0);
}

•static function Clamp (value : int, min : int, max : int) : int

Description描述

Clamps value between min and max and returns value.

限制value的值在min和max之间,并返回value。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public void Awake() {
		print(Mathf.Clamp(10, 1, 3));
	}
}
// Clamps the value 10 to be between 1 and 3.
// prints 3 to the console
//现在10在1~3之间,3被打印到控制台

print(Mathf.Clamp(10, 1, 3));
Page last updated: 2011-2-23