1 #pragma strict 2 import UnityEngine.UI; 3 4 function Start () { 5 6 } 7 var speed : int = 5; 8 var newobject : Transform; 9 var sdshu : int = 0;10 var xmshu : int = 0;11 12 13 function Update () {14 //yidong15 var x : float = Input.GetAxis("Horizontal")*Time.deltaTime*speed;16 var z : float = Input.GetAxis("Vertical")*Time.deltaTime*speed;17 if (Input.GetKey(KeyCode.H)){18 transform.Translate(0,5*Time.deltaTime,0);19 }20 if (Input.GetKey(KeyCode.N)){21 transform.Translate(0,-5*Time.deltaTime,0);22 }23 transform.Translate(x,0,z);24 //print (x);25 //kaihuo26 if (Input.GetButtonDown("Fire1")){27 var n : Transform = Instantiate(newobject,transform.position,transform.rotation);28 var fwd : Vector3 = transform.TransformDirection(Vector3.forward);29 n.GetComponent(Rigidbody).AddForce(fwd*3000);30 sdshu++;31 gameObject.Find("Canvas/wenzi").GetComponent(Text).text="射弹数:" +sdshu+ " 消灭数:" + xmshu;32 33 }34 //xuanzhuan35 if (Input.GetKey(KeyCode.Q)){36 transform.Rotate(0,-25*Time.deltaTime,0,Space.Self);37 }38 if(Input.GetKey(KeyCode.E)){39 transform.Rotate(0,25*Time.deltaTime,0,Space.Self);40 }41 if (Input.GetKey(KeyCode.Z)){42 transform.Rotate(-25*Time.deltaTime,0,0,Space.Self);43 }44 if (Input.GetKey(KeyCode.C)){45 transform.Rotate(25*Time.deltaTime,0,0,Space.Self);46 }47 48 }
这是射击脚本。射击脚本的另外一种写法:var newobject: Regidbody; var n: Rigidbody = Instantiate(newobject,transform.position,transform.rotation); n.velcocity = transform.forward*30.0;给n命名用于碰撞检测, n = “射弹”;
另一个鼠标点击函数:OnMouseDown()。
1 function Start () { 2 3 } 4 var sd : int; 5 var xm : int; 6 function Update () { 7 if(gameObject.transform.position.y<0){ 8 xm = gameObject.Find("Camera").GetComponent(sheji).xmshu++; 9 sd = gameObject.Find("Camera").GetComponent(sheji).sdshu;10 gameObject.Find("Canvas/wenzi").GetComponent(Text).text="射弹数:" +sd+ " 消灭数:" + xm;11 if (xm > 20){12 gameObject.Find("Canvas/wenzi").GetComponent(Text).text="恭喜过关!";13 gameObject.Find("Camera").GetComponent(sheji).enabled=false;14 gameObject.Find("dimian").GetComponent(restar).enabled=true;15 }16 Destroy(gameObject);17 }18 19 }
这是检查脚本。
1 #pragma strict 2 @script RequireComponent(AudioSource) 3 import UnityEngine.SceneManagement ; 4 5 function OnGUI(){ 6 var audio: AudioSource = GetComponent.(); 7 if(GUI.Button(Rect(180,100,60,30),"退 出")){ 8 Application.Quit(); 9 }10 if(GUI.Button(Rect(280,100,60,30),"重新开始")){11 SceneManager.LoadScene(SceneManager.GetActiveScene().name);12 }13 if(GUI.Button(Rect(10,160,100,50),"paly"))14 audio.Play();15 if(GUI.Button(Rect(10,220,100,50),"Pause"))16 audio.Pause();17 if(GUI.Button(Rect(10,280,100,50),"Stop"))18 audio.Stop();19 }
这是重启脚本,@强制获取组件。
var audio: AudioSource = GetComponent.<AudioSource>();可以不设置变量audio, GetComponent.<AudioSource>().Pause();这样使用。
1 #pragma strict2 import UnityEngine.UI;3 4 function OnCollisionEnter(){5 gameObject.Find("Canvas/wenzi").GetComponent(Text).text = "大玉螺旋丸!";6 gameObject.Find("Point light").GetComponent(Light).enabled = false;7 gameObject.Find("Spotlight").GetComponent(Light).enabled = false;8 Destroy(gameObject);9 }
这是碰撞检测脚本。可以加入实参col:Collider
1 #pragma strict2 3 function OnGUI(){4 if(GUI.Button(Rect(180,400,60,30),"开始游戏")){5 Application.Quit();6 }7 }
这里说明下按钮的使用代码。SceneManager.LoadScene(SceneManager.GetActiveScene().name);可以在loadScene实参直接使用“场景名字“。
GetComponent.<Renderer>().material.color = Color.green;U3D会自动更新过时代码。