unity中简单的js脚本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;
}
随机函数
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>().d; }
function Update () {
}
事件
}
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);
}
刚体中的脚本
function OnTriggerEnter(other:Collider)
{
Destroy(other.gameObject);
}
//要开启 is Trigger 出发碰撞
//⿏标按下就增加⼒
function FixedUpdate()
js脚本编程入门{
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);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。