GUI腳本
#pragma strict
var probArray:float[];
private var probValue:int;
function OnGUI()
{
if(GUI.Button(Rect(10,70,50,30),"Click"))
{
probValue=Choose(probArray);
switch(probValue)
{
case 1:
Debug.Log("npc向我敬禮");
break;
case 2:
Debug.Log("npc向我離開");
break;
default:
Debug.Log("nothing");
break;
}
}
}
function Start () {
}
function Update () {
}
function Choose(probs:float[])
{
var total=0.0;
for(elem in probs)
{
total+=elem;
}
var randomPoint =Random.value*total;
var i:int;
for(i=0;i<probs.Length;i++)
{
if(randomPoint<probs[i])
{
return i;
}
else
{
randomPoint-=probs[i];
}
}
return probs.Length-1;
}
隨機函數
#pragma strict
function Start () {
var a:float;
a=Random.value;
var b:int;
var c:float;
b=Random.Range(1,100);
c=Random.Range(0.0,10.0);
Debug.Log("a的值爲 "+a+"\n b:"+b+"c: "+c);
}
function Update () {
}
Time函數
#pragma strict
function Start () {
Do();
print("print");
}
function Update () {
GetComponent.<Light>().range+=20.0*Time.deltaTime;
}
function OnGUI()
{
GUILayout.Label("當前時間: "+Time.time);
GUILayout.Label("上一幀時間 : "+Time.deltaTime);
}
function Do()
{
Debug.Log(Time.time);
yield WaitForSeconds(5.0);
Debug.Log(Time.time);
}
object函數
#pragma strict
var target:GameObject;
function Start () {
target=GameObject.FindGameObjectWithTag("Finish");
// target=GameObject.Find("/Main Camera/Sphere");
target.GetComponent.<Renderer>().material.color=Color.red;
}
function Update () {
}
事件
#pragma strict
function Start () {
}
function Update () {
}
function OnGUI()
{
var e:Event=Event.current;
if(e.button==0&&e.isMouse)
{
Debug.Log("鼠標左鍵\n");
}
if(Input.GetKey("down"))
{
print("按下down\n");
}
if(Input.GetKey(KeyCode.UpArrow))
{
print("UpArrow鍵\n");
}
if(Input.GetButtonDown("Fire1"))
{
print("fire\n");
}
}
虛擬軸控制移動
#pragma strict
var speed:float=10.0;
var rotationSpeed:float=100.0;
function Update () {
var translation:float=Input.GetAxis("Vertical")*speed;
var rotation:float=Input.GetAxis("Horizontal")*rotationSpeed;
translation *=Time.deltaTime;
rotation *=Time.deltaTime;
transform.Translate(0,0,translation);
transform.Rotate(0,rotation,0);
}
剛體中的腳本
#pragma strict
function OnTriggerEnter(other:Collider)
{
Destroy(other.gameObject);
}
//要開啓 is Trigger 出發碰撞
//鼠標按下就 增加力
function FixedUpdate()
{
if(Input.GetMouseButton(0))
{
GetComponent.<Rigidbody>().AddForce(0,100,0);
}
}
//一般添加在主角里
function OnCollisionEnter(collision:Collision)
{
print(collision.gameObject);//打印被撞的物體
if(collision.rigidbody)
{
collision.rigidbody.AddForce(0.0,5.5,0.0);
}
}