博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
unity3d 射弹基础案例代码分析
阅读量:4315 次
发布时间:2019-06-06

本文共 3815 字,大约阅读时间需要 12 分钟。

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会自动更新过时代码。

 

 

 

转载于:https://www.cnblogs.com/white-L/p/6150385.html

你可能感兴趣的文章
Java 流(Stream)、文件(File)和IO
查看>>
几道汇编入门题目(二)
查看>>
系统架构师学习笔记_第一章_连载
查看>>
好文推荐!帮大忙了
查看>>
Sublime text3 016 SublimeLinter(PHP 代码检测)
查看>>
Python补充02 Python小技巧
查看>>
个人项目制作(PSP)
查看>>
【转】完美解决iphone连电脑蓝牙出现bluetooth外围设备无法正确安装
查看>>
有关TabNavigation的方式【项目】
查看>>
C#设计模式-访问者模式
查看>>
CS round--36
查看>>
Microsoft patterns & practices 学习笔记(0)
查看>>
python之路_前端之HTML初始
查看>>
UML基础:统一建模语言简介
查看>>
Oozie安装的说明
查看>>
2 weekend110的SecureCRTPortable远程连接 + 上传安装jdk + 上传安装配置hadoop
查看>>
【BZOJ-2733】永无乡 Splay+启发式合并
查看>>
Common Subsequence(最长公共子序列)
查看>>
weighing scheme
查看>>
java_简单解析ArrayList_iterable
查看>>