AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > Unity3D

Unity 2D坦克游戏教程(10):发射

51自学网 2014-05-24 http://www.51zixue.net

本文是Unity 2D 坦克游戏教程的第十部分,主要是继续前面的Unity3D坦克游戏,讲解如何完成发射的效果。

在此处下载这个工程文件包:发射

本教程涉及到:

  • 瞄准,发射以及用子弹消灭敌人!

先写脚本

现在我们必须先升级我们的实际上用来发射子弹的炮塔。

打开TurretController的脚本,然后修改成如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using UnityEngine;
using System.Collections;
 
public class TurretController : MonoBehaviour {
 
private Vector3 mouse_pos;
private Vector3 object_pos;
private float angle;
private float bulletSpeed = 500;
public GameObject[] ammo; // Array of enemy prefabs.
 
// Use this for initialization
void Start () {
}
 
void Update(){
}
 
void FixedUpdate () {
// Point the cannon at the mouse.
mouse_pos = Input.mousePosition;
mouse_pos.z = 0.0f;
object_pos = Camera.main.WorldToScreenPoint(transform.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg - 90;
Vector3 rotationVector = new Vector3 (0, 0, angle);
transform.rotation = Quaternion.Euler(rotationVector);
 
// Fire a bullet.
if(Input.GetMouseButtonDown(0)){
int ammoIndex = 0;
GameObject bullet = (GameObject)Instantiate(ammo[ammoIndex], transform.position, transform.rotation);
bullet.transform.LookAt(mouse_pos);
bullet.rigidbody2D.AddForce(bullet.transform.forward * bulletSpeed);
}
}
}

在每一次左击鼠标是这将会产生一些名为“Ammo”物体和提供朝着鼠标方向发射子弹力量。

现在我们开始制作子弹。在这个工程项目中,这里有一个叫“Pew”的红色的小方块图片,我们把它作为简单的子弹。这张图片可以随时使用,但需要创建一个新的图形GameObject,并把它设置为如下:

注意这个子弹有一个圆形的碰撞区域,一个图形和刚体嵌套在同样名字为“Pew”的游戏物体和“Pew”图层。子弹就像下图所示(那个绿色的线条就是圆形的碰撞区域):

现在拖拽子弹到我们的project面板,并把它转换成后放在Prefab里面。把它从场景中删除,我们只有在鼠标点击的时候才产生子弹。

Alien修改

在发射子弹钱,我们必须修改Alien Controller的脚本去寻找子弹的影响力,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using UnityEngine;
using System.Collections;
 
public class AlienController : MonoBehaviour {
 
public float alienSpeed = .02f;
private ScoreController score;
 
// Use this for initialization
void Start () {
score = GameObject.Find("Score").GetComponent();
}
 
// Update is called once per frame
void Update () {
transform.position += new Vector3(0, -alienSpeed, 0);
}
 
void OnTriggerEnter2D(Collider2D col)
{
// If the alien hits the trigger...
if (col.gameObject.tag == "Player") {
Destroy (gameObject);
 
if(score.score >= 100)
score.score -= 100;
else if(score.score > 0)
score.score = 0;
 
} else if (col.gameObject.tag == "Terrain") {
score.score += 100;
Destroy (gameObject);
}else if (col.gameObject.tag == "Pew") {
score.score += 100;
Destroy (gameObject);
}
}
 
}

现在,让我们按照下面的提示配置TurretController(添加弹药的预设来收集弹药,之后设置弹药的数量为1):

当我们运行环境,可以得到以下结果:

原文链接:http://www.unit3y.com/unity-2d-tank-game-part-x-shooting/


建议使用电驴(eMule)下载分享的资源。

说明
:本教程来源互联网或网友分享或出版商宣传分享,仅为学习研究或媒体推广,51zixue.net不保证资料的完整性。
 
上一篇:Unity 2D 坦克游戏教程(9):创建敌人  下一篇:Unity 2D坦克游戏教程