unity3d 射弹基础案例代码分析

时间:2022-11-13 04:10:24
 #pragma strict
import UnityEngine.UI; function Start () { }
var speed : int = 5;
var newobject : Transform;
var sdshu : int = 0;
var xmshu : int = 0; function Update () {
//yidong
var x : float = Input.GetAxis("Horizontal")*Time.deltaTime*speed;
var z : float = Input.GetAxis("Vertical")*Time.deltaTime*speed;
if (Input.GetKey(KeyCode.H)){
transform.Translate(0,5*Time.deltaTime,0);
}
if (Input.GetKey(KeyCode.N)){
transform.Translate(0,-5*Time.deltaTime,0);
}
transform.Translate(x,0,z);
//print (x);
//kaihuo
if (Input.GetButtonDown("Fire1")){
var n : Transform = Instantiate(newobject,transform.position,transform.rotation);
var fwd : Vector3 = transform.TransformDirection(Vector3.forward);
n.GetComponent(Rigidbody).AddForce(fwd*3000);
sdshu++;
gameObject.Find("Canvas/wenzi").GetComponent(Text).text="射弹数:" +sdshu+ " 消灭数:" + xmshu; }
//xuanzhuan
if (Input.GetKey(KeyCode.Q)){
transform.Rotate(0,-25*Time.deltaTime,0,Space.Self);
}
if(Input.GetKey(KeyCode.E)){
transform.Rotate(0,25*Time.deltaTime,0,Space.Self);
}
if (Input.GetKey(KeyCode.Z)){
transform.Rotate(-25*Time.deltaTime,0,0,Space.Self);
}
if (Input.GetKey(KeyCode.C)){
transform.Rotate(25*Time.deltaTime,0,0,Space.Self);
} }

这是射击脚本。射击脚本的另外一种写法:var newobject: Regidbody;         var n: Rigidbody = Instantiate(newobject,transform.position,transform.rotation);          n.velcocity = transform.forward*30.0;给n命名用于碰撞检测, n = “射弹”;

另一个鼠标点击函数:OnMouseDown()。

 function Start () {

 }
var sd : int;
var xm : int;
function Update () {
if(gameObject.transform.position.y<0){
xm = gameObject.Find("Camera").GetComponent(sheji).xmshu++;
sd = gameObject.Find("Camera").GetComponent(sheji).sdshu;
gameObject.Find("Canvas/wenzi").GetComponent(Text).text="射弹数:" +sd+ " 消灭数:" + xm;
if (xm > 20){
gameObject.Find("Canvas/wenzi").GetComponent(Text).text="恭喜过关!";
gameObject.Find("Camera").GetComponent(sheji).enabled=false;
gameObject.Find("dimian").GetComponent(restar).enabled=true;
}
Destroy(gameObject);
} }

这是检查脚本。

 #pragma strict
@script RequireComponent(AudioSource)
import UnityEngine.SceneManagement ; function OnGUI(){
var audio: AudioSource = GetComponent.<AudioSource>();
if(GUI.Button(Rect(180,100,60,30),"退 出")){
Application.Quit();
}
if(GUI.Button(Rect(280,100,60,30),"重新开始")){
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
if(GUI.Button(Rect(10,160,100,50),"paly"))
audio.Play();
if(GUI.Button(Rect(10,220,100,50),"Pause"))
audio.Pause();
if(GUI.Button(Rect(10,280,100,50),"Stop"))
audio.Stop();
}

这是重启脚本,@强制获取组件。

var audio: AudioSource = GetComponent.<AudioSource>();可以不设置变量audio, GetComponent.<AudioSource>().Pause();这样使用。

 #pragma strict
import UnityEngine.UI; function OnCollisionEnter(){
gameObject.Find("Canvas/wenzi").GetComponent(Text).text = "大玉螺旋丸!";
gameObject.Find("Point light").GetComponent(Light).enabled = false;
gameObject.Find("Spotlight").GetComponent(Light).enabled = false;
Destroy(gameObject);
}

这是碰撞检测脚本。可以加入实参col:Collider

 #pragma strict

 function  OnGUI(){
if(GUI.Button(Rect(180,400,60,30),"开始游戏")){
Application.Quit();
}
}

这里说明下按钮的使用代码。SceneManager.LoadScene(SceneManager.GetActiveScene().name);可以在loadScene实参直接使用“场景名字“。

GetComponent.<Renderer>().material.color = Color.green;U3D会自动更新过时代码。