unity中简单的js脚本

来源:互联网 发布:淘宝开店宝软件 编辑:程序博客网 时间:2024/05/28 18:43

GUI脚本

#pragma strictvar 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 strictfunction 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 strictfunction 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 strictvar target:GameObject;function Start () {    target=GameObject.FindGameObjectWithTag("Finish");    //   target=GameObject.Find("/Main Camera/Sphere");    target.GetComponent.<Renderer>().material.color=Color.red;}function Update () {    }


事件

#pragma strictfunction 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 strictvar 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 strictfunction 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);        }}




0 0
原创粉丝点击