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
随机函数
#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.().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.().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.().AddForce(0,100,0);
}
}
//一般添加在主角里
function OnCollisionEnter(collision:Collision)
{
print(collision.gameObject);//打印被撞的物体
if(collision.rigidbody)
{
collision.rigidbody.AddForce(0.0,5.5,0.0);
}
}